|
| 1 | +package sysdig |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "io/ioutil" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/falcosecurity/kilt/runtimes/cloudformation/cfnpatcher" |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | +) |
| 12 | + |
| 13 | +func TestECStransformation(t *testing.T) { |
| 14 | + inputfile, err := ioutil.ReadFile("testfiles/ECSinput.json") |
| 15 | + |
| 16 | + if err != nil { |
| 17 | + t.Fatalf("Cannot find testfiles/ECSinput.json") |
| 18 | + } |
| 19 | + |
| 20 | + recipeConfig := KiltRecipeConfig{ |
| 21 | + SysdigAccessKey: "sysdig_access_key", |
| 22 | + AgentImage: "workload_agent_image", |
| 23 | + OrchestratorHost: "orchestrator_host", |
| 24 | + OrchestratorPort: "orchestrator_port", |
| 25 | + CollectorHost: "collector_host", |
| 26 | + CollectorPort: "collector_port", |
| 27 | + } |
| 28 | + |
| 29 | + jsonConf, err := json.Marshal(&recipeConfig) |
| 30 | + if err != nil { |
| 31 | + t.Fatalf("Failed to serialize configuration: %v", err.Error()) |
| 32 | + } |
| 33 | + |
| 34 | + kiltConfig := &cfnpatcher.Configuration{ |
| 35 | + Kilt: agentinoKiltDefinition, |
| 36 | + ImageAuthSecret: "image_auth_secret", |
| 37 | + OptIn: false, |
| 38 | + UseRepositoryHints: true, |
| 39 | + RecipeConfig: string(jsonConf), |
| 40 | + } |
| 41 | + |
| 42 | + patchedOutput, err := patchFargateTaskDefinition(context.Background(), string(inputfile), kiltConfig) |
| 43 | + if err != nil { |
| 44 | + t.Fatalf("Cannot execute PatchFargateTaskDefinition : %v", err.Error()) |
| 45 | + } |
| 46 | + |
| 47 | + expectedOutput, err := ioutil.ReadFile("testfiles/ECSInstrumented.json") |
| 48 | + if err != nil { |
| 49 | + t.Fatalf("Cannot find testfiles/ECSinput.json") |
| 50 | + } |
| 51 | + |
| 52 | + type cdef struct { |
| 53 | + Command []string `json:"Command"` |
| 54 | + EntryPoint []string `json:"EntryPoint"` |
| 55 | + Environment []map[string]string `json:"Environment"` |
| 56 | + Image string `json:"Image"` |
| 57 | + Linuxparameters interface{} `json:"LinuxParameters"` |
| 58 | + VolumesFrom []interface{} `json:"VolumesFrom"` |
| 59 | + LogConfiguration interface{} `json:"LogConfiguration"` |
| 60 | + Name string `json:"Name"` |
| 61 | + Image2 string `json:"image"` |
| 62 | + EntryPoint2 string `json:"entryPoint"` |
| 63 | + } |
| 64 | + |
| 65 | + var patchedContainerDefinitions, expectedContainerDefinitions []cdef |
| 66 | + err = json.Unmarshal([]byte(*patchedOutput), &patchedContainerDefinitions) |
| 67 | + if err != nil { |
| 68 | + t.Fatalf("Error Unmarshaling patched Container definitions : %v", err.Error()) |
| 69 | + } |
| 70 | + |
| 71 | + err = json.Unmarshal([]byte(expectedOutput), &expectedContainerDefinitions) |
| 72 | + if err != nil { |
| 73 | + t.Fatalf("Error Unmarshaling expected Container definitions: %v", err.Error()) |
| 74 | + } |
| 75 | + |
| 76 | + assert.Equal(t, expectedContainerDefinitions[0].Name, patchedContainerDefinitions[0].Name) |
| 77 | + |
| 78 | + // The order received from patchedOutput changes continuously hence it is important to check if the arrays of expected and actual are equal without order being correct. This check also |
| 79 | + // helps with checking if key/value is named "Name" and "Value" accordingly. |
| 80 | + assert.ElementsMatch(t, expectedContainerDefinitions[0].Environment, patchedContainerDefinitions[0].Environment) |
| 81 | + |
| 82 | + // Check if Image key is correct |
| 83 | + assert.Equal(t, expectedContainerDefinitions[0].Image, patchedContainerDefinitions[0].Image) |
| 84 | + assert.Equal(t, patchedContainerDefinitions[0].Image2, "") |
| 85 | + |
| 86 | + // Check if entryPoint key is correct |
| 87 | + assert.Equal(t, expectedContainerDefinitions[0].EntryPoint, patchedContainerDefinitions[0].EntryPoint) |
| 88 | + assert.Equal(t, patchedContainerDefinitions[0].EntryPoint2, "") |
| 89 | +} |
0 commit comments