From d3065cfed186c4c62c5fa1f030f8abf2da662169 Mon Sep 17 00:00:00 2001 From: AhmedGrati Date: Sat, 29 Jul 2023 19:17:08 +0100 Subject: [PATCH] test: add unit tests for the expiration date function Signed-off-by: AhmedGrati --- docs/usage/customization-guide.md | 4 +++- source/local/local.go | 5 +++-- source/local/local_test.go | 21 +++++++++++++++++++++ 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/docs/usage/customization-guide.md b/docs/usage/customization-guide.md index bf641587a1..b17d2a269f 100644 --- a/docs/usage/customization-guide.md +++ b/docs/usage/customization-guide.md @@ -313,11 +313,13 @@ The label value defaults to `true`, if not specified. Label namespace may be specified with `/[=]`. -You can include the following block if you want to define the expiration date +You can include the following block if you want to define the expiration date of features that are described in the feature files: + ```plaintext # +expiry-time: 2023-07-29T11:22:33Z ``` + **Note: The time format that we are supporting is RFC3339.** ### Mounts diff --git a/source/local/local.go b/source/local/local.go index 99f8608c7e..c3f1bdeb5c 100644 --- a/source/local/local.go +++ b/source/local/local.go @@ -282,6 +282,7 @@ func getFeaturesFromFiles() (map[string]string, error) { } if expiryDate.Before(time.Now()) { + klog.InfoS("feature file is expired", "fileName", fileName) continue } @@ -304,9 +305,9 @@ func getExpirationDate(lines [][]byte) (time.Time, error) { lineSplit := strings.SplitN(string(line), ":", 2) key := lineSplit[0] - if key == ExpiryDateKey { - expiryDate, err := time.Parse(time.RFC3339, lineSplit[1]) + expiryDate, err := time.Parse(time.RFC3339, strings.TrimSpace(lineSplit[1])) + fmt.Println(err) if err != nil { return time.Now(), err } diff --git a/source/local/local_test.go b/source/local/local_test.go index e83493c63c..cfd45e447c 100644 --- a/source/local/local_test.go +++ b/source/local/local_test.go @@ -17,7 +17,9 @@ limitations under the License. package local import ( + "fmt" "testing" + "time" "github.com/stretchr/testify/assert" ) @@ -33,3 +35,22 @@ func TestLocalSource(t *testing.T) { assert.Empty(t, l) } + +func TestGetExpirationDate(t *testing.T) { + + expectedDate := "2023-07-28T11:22:33Z" + fileContent := []string{ + fmt.Sprintf("# +expiry-time: %v", expectedDate), + "featureKey=featureValue", + } + + fileContentByte := [][]byte{} + + for _, content := range fileContent { + fileContentByte = append(fileContentByte, []byte(content)) + } + + expirationDate, err := getExpirationDate(fileContentByte) + assert.Nil(t, err) + assert.Equal(t, expirationDate.Format(time.RFC3339), expectedDate) +}