Skip to content

Commit

Permalink
test: add unit tests for the expiration date function
Browse files Browse the repository at this point in the history
Signed-off-by: AhmedGrati <ahmedgrati1999@gmail.com>
  • Loading branch information
TessaIO committed Jul 29, 2023
1 parent b90d2ea commit d3065cf
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
4 changes: 3 additions & 1 deletion docs/usage/customization-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -313,11 +313,13 @@ The label value defaults to `true`, if not specified.

Label namespace may be specified with `<namespace>/<name>[=<value>]`.

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
Expand Down
5 changes: 3 additions & 2 deletions source/local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ func getFeaturesFromFiles() (map[string]string, error) {
}

if expiryDate.Before(time.Now()) {
klog.InfoS("feature file is expired", "fileName", fileName)
continue

Check warning on line 286 in source/local/local.go

View check run for this annotation

Codecov / codecov/patch

source/local/local.go#L284-L286

Added lines #L284 - L286 were not covered by tests
}

Expand All @@ -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
}

Check warning on line 313 in source/local/local.go

View check run for this annotation

Codecov / codecov/patch

source/local/local.go#L312-L313

Added lines #L312 - L313 were not covered by tests
Expand Down
21 changes: 21 additions & 0 deletions source/local/local_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ limitations under the License.
package local

import (
"fmt"
"testing"
"time"

"github.com/stretchr/testify/assert"
)
Expand All @@ -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)
}

0 comments on commit d3065cf

Please sign in to comment.