|
| 1 | +// Copyright 2021 - [email protected] |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package inventory |
| 16 | + |
| 17 | +import ( |
| 18 | + "io" |
| 19 | + "reflect" |
| 20 | + "strings" |
| 21 | + "testing" |
| 22 | +) |
| 23 | + |
| 24 | +// mockInventoryResponse returns an io.ReadCloser simulating data coming from an inventory endpoint |
| 25 | +func mockInventoryResponse(raw string) io.ReadCloser { |
| 26 | + return io.NopCloser(strings.NewReader(raw)) |
| 27 | +} |
| 28 | + |
| 29 | +func Test_parseInventory(t *testing.T) { |
| 30 | + type args struct { |
| 31 | + inventoryFormat string |
| 32 | + inventoryData io.ReadCloser |
| 33 | + } |
| 34 | + |
| 35 | + tests := []struct { |
| 36 | + name string |
| 37 | + args args |
| 38 | + want []Host |
| 39 | + wantErr bool |
| 40 | + }{ |
| 41 | + // Format: 'ndjson' |
| 42 | + { |
| 43 | + name: "Test single ndjson inventory entry", |
| 44 | + args: args{ |
| 45 | + inventoryFormat: "ndjson", |
| 46 | + inventoryData: mockInventoryResponse(` |
| 47 | + {"ip_address":"10.0.1.2","domain":"xyz.service.consul","hostgroup":"xyz"} |
| 48 | + `), |
| 49 | + }, |
| 50 | + want: []Host{ |
| 51 | + {IPAddress: "10.0.1.2", Domain: "xyz.service.consul", Hostgroup: "xyz"}, |
| 52 | + }, |
| 53 | + }, |
| 54 | + { |
| 55 | + name: "Test multiple ndjson inventory entries", |
| 56 | + args: args{ |
| 57 | + inventoryFormat: "ndjson", |
| 58 | + inventoryData: mockInventoryResponse(` |
| 59 | + {"ip_address":"10.0.1.2","domain":"xyz.service.consul","hostgroup":"xyz"} |
| 60 | + {"ip_address":"172.16.1.2","domain":"abc.service.consul","hostgroup":"abc"} |
| 61 | + `), |
| 62 | + }, |
| 63 | + want: []Host{ |
| 64 | + {IPAddress: "10.0.1.2", Domain: "xyz.service.consul", Hostgroup: "xyz"}, |
| 65 | + {IPAddress: "172.16.1.2", Domain: "abc.service.consul", Hostgroup: "abc"}, |
| 66 | + }, |
| 67 | + }, |
| 68 | + |
| 69 | + // Format: 'arrayjson' |
| 70 | + { |
| 71 | + name: "Test single arrayjson inventory entry", |
| 72 | + args: args{ |
| 73 | + inventoryFormat: "arrayjson", |
| 74 | + inventoryData: mockInventoryResponse(` |
| 75 | + [ |
| 76 | + {"ip_address":"10.0.1.2","domain":"xyz.service.consul","hostgroup":"xyz"} |
| 77 | + ] |
| 78 | + `), |
| 79 | + }, |
| 80 | + want: []Host{ |
| 81 | + {IPAddress: "10.0.1.2", Domain: "xyz.service.consul", Hostgroup: "xyz"}, |
| 82 | + }, |
| 83 | + }, |
| 84 | + { |
| 85 | + name: "Test multiple arrayjson inventory entries", |
| 86 | + args: args{ |
| 87 | + inventoryFormat: "arrayjson", |
| 88 | + inventoryData: mockInventoryResponse(` |
| 89 | + [ |
| 90 | + {"ip_address":"10.0.1.2","domain":"xyz.service.consul","hostgroup":"xyz"}, |
| 91 | + {"ip_address":"172.16.1.2","domain":"abc.service.consul","hostgroup":"abc"} |
| 92 | + ] |
| 93 | + `), |
| 94 | + }, |
| 95 | + want: []Host{ |
| 96 | + {IPAddress: "10.0.1.2", Domain: "xyz.service.consul", Hostgroup: "xyz"}, |
| 97 | + {IPAddress: "172.16.1.2", Domain: "abc.service.consul", Hostgroup: "abc"}, |
| 98 | + }, |
| 99 | + }, |
| 100 | + } |
| 101 | + for _, tt := range tests { |
| 102 | + t.Run(tt.name, func(t *testing.T) { |
| 103 | + got, err := parseInventory(tt.args.inventoryFormat, tt.args.inventoryData) |
| 104 | + if (err != nil) != tt.wantErr { |
| 105 | + t.Errorf("parseInventory() error = %v, wantErr %v", err, tt.wantErr) |
| 106 | + return |
| 107 | + } |
| 108 | + if !reflect.DeepEqual(got, tt.want) { |
| 109 | + t.Errorf("parseInventory() = %v, want %v", got, tt.want) |
| 110 | + } |
| 111 | + }) |
| 112 | + } |
| 113 | +} |
0 commit comments