|
18 | 18 | package confgen |
19 | 19 |
|
20 | 20 | import ( |
21 | | - "github.com/netobserv/flowlogs2metrics/pkg/api" |
22 | 21 | "github.com/stretchr/testify/require" |
23 | 22 | "os" |
| 23 | + "path" |
| 24 | + "path/filepath" |
24 | 25 | "testing" |
25 | 26 | ) |
26 | 27 |
|
27 | | -const testConfig = `--- |
28 | | -## This is the main configuration file for flowlogs2metrics. It holds |
29 | | -## all parameters needed for the creation of the configuration |
30 | | -## |
| 28 | +func getConfGen() *ConfGen { |
| 29 | + return &ConfGen{} |
| 30 | +} |
| 31 | + |
| 32 | +func Test_checkHeader(t *testing.T) { |
| 33 | + filename := "/tmp/header.check.txt" |
| 34 | + fakeFilename := "/tmp/fake_file.does.exist" |
| 35 | + wrongHeader := "#wrong_confgen" |
| 36 | + cg := getConfGen() |
| 37 | + err := cg.checkHeader(fakeFilename) |
| 38 | + require.Error(t, err) |
| 39 | + |
| 40 | + err = os.WriteFile(filename, []byte(wrongHeader), 0644) |
| 41 | + require.NoError(t, err) |
| 42 | + err = cg.checkHeader(filename) |
| 43 | + require.Error(t, err) |
| 44 | + |
| 45 | + err = os.WriteFile(filename, []byte(definitionHeader), 0644) |
| 46 | + require.NoError(t, err) |
| 47 | + err = cg.checkHeader(filename) |
| 48 | + require.NoError(t, err) |
| 49 | +} |
| 50 | + |
| 51 | +const networkDefinitionConfiguration = `#fl2m_confgen |
31 | 52 | description: |
32 | 53 | test description |
33 | | -ingest: |
34 | | - collector: |
35 | | - port: 8888 |
| 54 | +details: |
| 55 | + test details |
| 56 | +usage: |
| 57 | + test usage |
| 58 | +labels: |
| 59 | + - test |
| 60 | + - label |
| 61 | +transform: |
| 62 | + rules: |
| 63 | + - input: testInput |
| 64 | + output: testOutput |
| 65 | + type: add_service |
| 66 | + parameters: proto |
| 67 | +extract: |
| 68 | + aggregates: |
| 69 | + - name: test_aggregates |
| 70 | + by: |
| 71 | + - service |
| 72 | + operation: sum |
| 73 | + recordKey: test_record_key |
36 | 74 | encode: |
| 75 | + type: prom |
37 | 76 | prom: |
38 | | - port: 7777 |
39 | | - prefix: prefix |
| 77 | + metrics: |
| 78 | + - name: test_metric |
| 79 | + type: gauge |
| 80 | + valuekey: test_aggregates_value |
| 81 | + labels: |
| 82 | + - by |
| 83 | + - aggregate |
| 84 | +visualization: |
| 85 | + type: grafana |
| 86 | + grafana: |
| 87 | + - expr: 'test expression' |
| 88 | + type: graphPanel |
| 89 | + dashboard: test |
| 90 | + title: |
| 91 | + Test grafana title |
40 | 92 | ` |
41 | 93 |
|
42 | | -func getConfGen() *ConfGen { |
43 | | - return &ConfGen{} |
44 | | -} |
| 94 | +func Test_parseFile(t *testing.T) { |
| 95 | + fakeFilename := "/tmp/fake_file.does.exist" |
| 96 | + filename := "/tmp/parse_file.check.txt" |
| 97 | + cg := getConfGen() |
| 98 | + err := cg.parseFile(fakeFilename) |
| 99 | + require.Error(t, err) |
45 | 100 |
|
46 | | -func expectedConfig() *Config { |
47 | | - return &Config{ |
48 | | - Description: "test description", |
49 | | - Encode: ConfigEncode{ |
50 | | - Prom: api.PromEncode{ |
51 | | - Port: 7777, |
52 | | - Prefix: "prefix", |
53 | | - }, |
54 | | - }, |
55 | | - Ingest: ConfigIngest{ |
56 | | - Collector: api.IngestCollector{ |
57 | | - Port: 8888, |
58 | | - }, |
59 | | - }, |
60 | | - } |
| 101 | + err = os.WriteFile(filename, []byte(networkDefinitionConfiguration), 0644) |
| 102 | + require.NoError(t, err) |
| 103 | + err = cg.parseFile(filename) |
| 104 | + require.NoError(t, err) |
61 | 105 | } |
62 | 106 |
|
63 | | -func Test_parseConfigFile(t *testing.T) { |
64 | | - filename := "/tmp/config" |
| 107 | +func Test_getDefinitionFiles(t *testing.T) { |
| 108 | + dirPath := "/tmp/getDefinitionFilesTest" |
| 109 | + filename := "/def.yaml" |
65 | 110 | cg := getConfGen() |
66 | | - err := os.WriteFile(filename, []byte(testConfig), 0644) |
67 | | - require.Equal(t, err, nil) |
68 | | - config, err := cg.parseConfigFile(filename) |
69 | | - require.Equal(t, err, nil) |
70 | | - require.Equal(t, config, expectedConfig()) |
| 111 | + err := os.MkdirAll(dirPath, 0755) |
| 112 | + require.NoError(t, err) |
| 113 | + err = os.WriteFile(filepath.Join(dirPath, filename), []byte(networkDefinitionConfiguration), 0644) |
| 114 | + require.NoError(t, err) |
| 115 | + files := cg.getDefinitionFiles(dirPath) |
| 116 | + require.Equal(t, 1, len(files)) |
| 117 | + expected := []string{path.Join(dirPath, filename)} |
| 118 | + require.ElementsMatch(t, expected, files) |
| 119 | +} |
| 120 | + |
| 121 | +func Test_NewConfGen(t *testing.T) { |
| 122 | + _, err := NewConfGen() |
| 123 | + require.NoError(t, err) |
71 | 124 | } |
0 commit comments