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 Aug 2, 2023
1 parent b90d2ea commit 140528d
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 39 deletions.
10 changes: 7 additions & 3 deletions docs/usage/customization-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -313,12 +313,16 @@ 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
# +expiry-time=2023-07-29T11:22:33Z
```
**Note: The time format that we are supporting is RFC3339.**

**Note: The time format that we are supporting is RFC3339. Also, the `expiry-time`
tag is only evaluated in each re-discovery period, and the expiration of
node labels is not tracked.**

### Mounts

Expand Down
54 changes: 18 additions & 36 deletions source/local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func (s *localSource) GetFeatures() *nfdv1alpha1.Features {
return s.features
}

func parseFeatures(lines [][]byte) map[string]string {
func parseFeatures(lines [][]byte, fileName string) map[string]string {
features := make(map[string]string)

for _, line := range lines {
Expand All @@ -162,7 +162,21 @@ func parseFeatures(lines [][]byte) map[string]string {
if len(lineSplit) == 1 {
features[key] = "true"
} else {
features[key] = lineSplit[1]
value := lineSplit[1]
switch key {
case ExpiryDateKey:
expiryDate, err := time.Parse(time.RFC3339, strings.TrimSpace(value))
if err != nil {
klog.ErrorS(err, "failed to parse feature file expiry date", "fileName", fileName)
continue
}
if expiryDate.Before(time.Now()) {
klog.InfoS("feature file is expired", "fileName", fileName)
return map[string]string{}
}
default:
features[key] = value
}
}
}
}
Expand Down Expand Up @@ -196,7 +210,7 @@ func getFeaturesFromHooks() (map[string]string, error) {
}

// Append features
fileFeatures := parseFeatures(lines)
fileFeatures := parseFeatures(lines, fileName)

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

View check run for this annotation

Codecov / codecov/patch

source/local/local.go#L213

Added line #L213 was not covered by tests
klog.V(4).InfoS("hook executed", "fileName", fileName, "features", utils.DelayedDumper(fileFeatures))
for k, v := range fileFeatures {
if old, ok := features[k]; ok {
Expand Down Expand Up @@ -272,18 +286,7 @@ func getFeaturesFromFiles() (map[string]string, error) {
}

// Append features
fileFeatures := parseFeatures(lines)

// Check expiration of file features
expiryDate, err := getExpirationDate(lines)
if err != nil {
klog.ErrorS(err, "failed to parse feature file expiry date", "fileName", fileName)
continue
}

if expiryDate.Before(time.Now()) {
continue
}
fileFeatures := parseFeatures(lines, fileName)

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

View check run for this annotation

Codecov / codecov/patch

source/local/local.go#L289-L290

Added lines #L289 - L290 were not covered by tests
klog.V(4).InfoS("feature file read", "fileName", fileName, "features", utils.DelayedDumper(fileFeatures))
for k, v := range fileFeatures {
Expand All @@ -297,27 +300,6 @@ func getFeaturesFromFiles() (map[string]string, error) {
return features, nil
}

// Return the expiration date of a feature file
func getExpirationDate(lines [][]byte) (time.Time, error) {
for _, line := range lines {
if len(line) > 0 {
lineSplit := strings.SplitN(string(line), ":", 2)

key := lineSplit[0]

if key == ExpiryDateKey {
expiryDate, err := time.Parse(time.RFC3339, lineSplit[1])
if err != nil {
return time.Now(), err
}
return expiryDate, nil
}
}
}

return time.Now(), nil
}

// Read one file
func getFileContent(fileName string) ([][]byte, error) {
var lines [][]byte
Expand Down
43 changes: 43 additions & 0 deletions source/local/local_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package local

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -33,3 +34,45 @@ func TestLocalSource(t *testing.T) {
assert.Empty(t, l)

}

func TestGetExpirationDate(t *testing.T) {

testCases := []struct {
name string
expiryDate string
expectedFeaturesLen int
}{
{
name: "valid feature file",
expiryDate: "2080-07-28T11:22:33Z",
expectedFeaturesLen: 1,
},
{
name: "expired feature file",
expiryDate: "2012-07-28T11:22:33Z",
expectedFeaturesLen: 0,
},
{
name: "unparsable expiryDate",
expiryDate: "2080-07-28T11:22:33X",
expectedFeaturesLen: 1,
},
}

for _, tc := range testCases {
fileContent := []string{
fmt.Sprintf("# +expiry-time=%v", tc.expiryDate),
"featureKey=featureValue",
}

fileContentByte := [][]byte{}

for _, content := range fileContent {
fileContentByte = append(fileContentByte, []byte(content))
}

features := parseFeatures(fileContentByte, "testfile")
assert.Equal(t, tc.expectedFeaturesLen, len(features))

}
}

0 comments on commit 140528d

Please sign in to comment.