Skip to content

Commit

Permalink
Expiry Range / Testing
Browse files Browse the repository at this point in the history
Add functions to check for expiration within a range,
this could be used to set custom flags or automation.
  • Loading branch information
rossedman committed Sep 29, 2021
1 parent e597b92 commit 7beddc0
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 1 deletion.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,7 @@ install: build
mv bin/kubectl-check /usr/local/bin/kubectl-check

test:
go test -coverprofile=coverage.out -race -v ./...
go test -coverprofile=coverage.out -race -v ./...

coverage:
go tool cover -html=coverage.out
12 changes: 12 additions & 0 deletions pkg/endoflife/endoflife_eks.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ func (a *AmazonEKS) GetDaysUntilEnd() (float64, error) {
return math.Round(endDate.Sub(today).Hours() / 24), nil
}

// InExpiryRange allows program to provide a timeframe that
// is considered "failed". Maybe the end of life deadline is 30
// days away but with a threshold of 60 days you would consider
// that failed
func (a *AmazonEKS) InExpiryRange(days int) (bool, error) {
d, err := a.GetDaysUntilEnd()
if err != nil {
return false, err
}
return d < float64(days), nil
}

// GetAmazonEKS returns the data for a single release of EKS
func (c *Client) GetAmazonEKS(version string) (AmazonEKS, error) {
res := AmazonEKS{}
Expand Down
33 changes: 33 additions & 0 deletions pkg/endoflife/endoflife_eks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,43 @@ import (
"net/http"
"net/http/httptest"
"testing"
"time"

"github.com/stretchr/testify/assert"
)

func TestAmazonEKS(t *testing.T) {
t.Run("returns days until end", func(t *testing.T) {
now := time.Now()
eks := AmazonEKS{
EOL: now.AddDate(0, 0, 8).Format("2006-01-02"),
}
days, err := eks.GetDaysUntilEnd()
assert.Nil(t, err)
assert.Equal(t, float64(7), days)
})

t.Run("within expiry range returns true", func(t *testing.T) {
now := time.Now()
eks := AmazonEKS{
EOL: now.AddDate(0, 0, 25).Format("2006-01-02"),
}
thres, err := eks.InExpiryRange(30)
assert.Nil(t, err)
assert.True(t, thres)
})

t.Run("within expiry range returns false when more than range", func(t *testing.T) {
now := time.Now()
eks := AmazonEKS{
EOL: now.AddDate(0, 0, 60).Format("2006-01-02"),
}
thres, err := eks.InExpiryRange(30)
assert.Nil(t, err)
assert.False(t, thres)
})
}

func TestGetEKS(t *testing.T) {
t.Run("retrieves response", func(t *testing.T) {
// set mock testing server
Expand Down
12 changes: 12 additions & 0 deletions pkg/endoflife/endoflife_kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ func (k *Kubernetes) GetDaysUntilEnd() (float64, error) {
return math.Round(endDate.Sub(today).Hours() / 24), nil
}

// InExpiryRange allows program to provide a timeframe that
// is considered "failed". Maybe the end of life deadline is 30
// days away but with a threshold of 60 days you would consider
// that failed
func (k *Kubernetes) InExpiryRange(days int) (bool, error) {
d, err := k.GetDaysUntilEnd()
if err != nil {
return false, err
}
return d < float64(days), nil
}

// GetKubernetes returns the data for a single release of Kubernetes
func (c *Client) GetKubernetes(version string) (Kubernetes, error) {
res := Kubernetes{}
Expand Down
33 changes: 33 additions & 0 deletions pkg/endoflife/endoflife_kubernetes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,43 @@ import (
"net/http"
"net/http/httptest"
"testing"
"time"

"github.com/stretchr/testify/assert"
)

func TestKubernetes(t *testing.T) {
t.Run("returns days until end", func(t *testing.T) {
now := time.Now()
eks := Kubernetes{
EOL: now.AddDate(0, 0, 8).Format("2006-01-02"),
}
days, err := eks.GetDaysUntilEnd()
assert.Nil(t, err)
assert.Equal(t, float64(7), days)
})

t.Run("within expiry range returns true when less than range", func(t *testing.T) {
now := time.Now()
eks := Kubernetes{
EOL: now.AddDate(0, 0, 25).Format("2006-01-02"),
}
thres, err := eks.InExpiryRange(30)
assert.Nil(t, err)
assert.True(t, thres)
})

t.Run("within expiry range returns false when more than range", func(t *testing.T) {
now := time.Now()
eks := Kubernetes{
EOL: now.AddDate(0, 0, 60).Format("2006-01-02"),
}
thres, err := eks.InExpiryRange(30)
assert.Nil(t, err)
assert.False(t, thres)
})
}

func TestGetKubernetes(t *testing.T) {
t.Run("retrieves response", func(t *testing.T) {
// set mock testing server
Expand Down

0 comments on commit 7beddc0

Please sign in to comment.