Skip to content

Commit

Permalink
fix reading rules from file (#89)
Browse files Browse the repository at this point in the history
  • Loading branch information
vitorvezani authored Nov 4, 2022
1 parent b4292a9 commit 413a4eb
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 2 deletions.
4 changes: 2 additions & 2 deletions pkg/rules/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,16 +155,16 @@ func BuildRulesTree(org, token, hostName string, tree treeprint.Tree) error {
func getRulesFromFiles(files map[string][]string) ([]Rule, error) {
var rules []Rule
for _, ruleFiles := range files {
var rule Rule
for _, filePath := range ruleFiles {
fileContents, err := os.ReadFile(filePath)
if err != nil {
logrus.Error(err, "Error reading file", filePath)
return nil, err
}
var rule Rule
err = yaml.Unmarshal(fileContents, &rule)
if err != nil {
logrus.Error(err, "Error Unmarshalling check YAML", filePath)
logrus.Error(err, "Error unmarshalling check YAML", filePath)
return nil, err
}
if rule.Name == "" {
Expand Down
33 changes: 33 additions & 0 deletions pkg/rules/rules_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2022 FairwindsOps Inc
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package rules

import (
"testing"

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

// FWI-3100 - the rule variable was not being reset - so previous values were being re-used
func TestGetRulesFromFiles(t *testing.T) {
folderRules := map[string][]string{
"testdata": {"testdata/rule1.yaml", "testdata/rule2.yaml"},
}
rules, err := getRulesFromFiles(folderRules)
assert.NoError(t, err)

assert.Equal(t, "us-east-1", rules[0].Cluster)
assert.Equal(t, "", rules[1].Cluster)
}
7 changes: 7 additions & 0 deletions pkg/rules/testdata/rule1.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
name: "rule-1"
description: "Assigns all Action Items in the api namespace to [email protected]"
cluster: us-east-1
action: |
if (ActionItem.ResourceNamespace === 'api') {
ActionItem.AssigneeEmail = '[email protected]';
}
7 changes: 7 additions & 0 deletions pkg/rules/testdata/rule2.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
name: "rule-2"
description: "Assigns all Action Items in the api namespace to [email protected]"
cluster: null
action: |
if (ActionItem.ResourceNamespace === 'api') {
ActionItem.AssigneeEmail = '[email protected]';
}

0 comments on commit 413a4eb

Please sign in to comment.