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 5, 2023
1 parent f0edc65 commit 0e00623
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 40 deletions.
9 changes: 6 additions & 3 deletions docs/usage/customization-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -316,13 +316,16 @@ Label namespace may be specified with `<namespace>/<name>[=<value>]`.

Comment lines (starting with `#`) are ignored.

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
73 changes: 36 additions & 37 deletions source/local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,17 +149,37 @@ func (s *localSource) GetFeatures() *nfdv1alpha1.Features {
return s.features
}

func parseComments(lines [][]byte, fileName string) map[string]interface{} {
comments := make(map[string]interface{})
for _, l := range lines {
line := strings.TrimSpace(string(l))
if len(line) > 0 && strings.HasPrefix(line, "#") {
lineSplit := strings.SplitN(line, "=", 2)

key := lineSplit[0]
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
}
comments[ExpiryDateKey] = expiryDate
default:
klog.ErrorS(fmt.Errorf("unknown comment encountered while parsing features' comments"), "unknown comment", "commentKey", key)

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

View check run for this annotation

Codecov / codecov/patch

source/local/local.go#L169-L170

Added lines #L169 - L170 were not covered by tests
}
}
}
return comments
}

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

for _, l := range lines {
line := strings.TrimSpace(string(l))
if len(line) > 0 {
// Skip comment lines
if strings.HasPrefix(line, "#") {
continue
}

if len(line) > 0 && !strings.HasPrefix(line, "#") {
lineSplit := strings.SplitN(line, "=", 2)

key := lineSplit[0]
Expand Down Expand Up @@ -277,19 +297,19 @@ func getFeaturesFromFiles() (map[string]string, error) {
continue
}

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

// 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
// handle expiry date
if expiryDate, ok := comments[ExpiryDateKey]; ok {
if expiryDate.(time.Time).Before(time.Now()) {
klog.InfoS("feature file is expired", "fileName", fileName)
continue
}
}

if expiryDate.Before(time.Now()) {
continue
}
// Append features
fileFeatures := parseFeatures(lines)

klog.V(4).InfoS("feature file read", "fileName", fileName, "features", utils.DelayedDumper(fileFeatures))
for k, v := range fileFeatures {
Expand All @@ -303,27 +323,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
13 changes: 13 additions & 0 deletions source/local/local_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ limitations under the License.
package local

import (
"os"
"path/filepath"
"testing"

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

}

func TestGetExpirationDate(t *testing.T) {
expectedFeaturesLen := 1
pwd, _ := os.Getwd()
featureFilesDir = filepath.Join(pwd, "testdata/features.d")

features, err := getFeaturesFromFiles()

assert.NoError(t, err)
assert.Equal(t, expectedFeaturesLen, len(features))
}
2 changes: 2 additions & 0 deletions source/local/testdata/features.d/expired_feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# +expiry-time=2012-07-28T11:22:33Z
featureKey=featureValue
2 changes: 2 additions & 0 deletions source/local/testdata/features.d/unparsable_expiry_comment
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# +expiry-time=2080-07-28T11:22:33X
featureKey=featureValue
2 changes: 2 additions & 0 deletions source/local/testdata/features.d/valid_feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# +expiry-time=2080-07-28T11:22:33Z
featureKey=featureValue

0 comments on commit 0e00623

Please sign in to comment.