|
| 1 | +package project |
| 2 | + |
| 3 | +import ( |
| 4 | + "io/ioutil" |
| 5 | + "os" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | +) |
| 10 | + |
| 11 | +func TestParseV3WithOneFile(t *testing.T) { |
| 12 | + // set up file |
| 13 | + numOfServices := 2 |
| 14 | + composeFileString := `version: '3' |
| 15 | +services: |
| 16 | + wordpress: |
| 17 | + image: wordpress |
| 18 | + ports: ["80:80"] |
| 19 | + mysql: |
| 20 | + image: mysql` |
| 21 | + |
| 22 | + tmpfile, err := ioutil.TempFile("", "test") |
| 23 | + if err != nil { |
| 24 | + t.Fatal("Unexpected error in creating test file: ", err) |
| 25 | + } |
| 26 | + defer os.Remove(tmpfile.Name()) |
| 27 | + |
| 28 | + if _, err := tmpfile.Write([]byte(composeFileString)); err != nil { |
| 29 | + t.Fatal("Unexpected error writing to test file: ", err) |
| 30 | + } |
| 31 | + if err := tmpfile.Close(); err != nil { |
| 32 | + t.Fatal("Unexpected error closing test file: ", err) |
| 33 | + } |
| 34 | + |
| 35 | + // add files to projects |
| 36 | + project := setupTestProject(t) |
| 37 | + project.ecsContext.ComposeFiles = append(project.ecsContext.ComposeFiles, tmpfile.Name()) |
| 38 | + |
| 39 | + // assert number of container configs matches expected number of services |
| 40 | + // TODO: assert containerConfig content after conversion implemented |
| 41 | + conConfigResult, err := project.parseV3() |
| 42 | + if err != nil { |
| 43 | + t.Fatal("Unexpected error parsing file: ", err) |
| 44 | + } |
| 45 | + assert.Equal(t, numOfServices, len(*conConfigResult)) |
| 46 | +} |
| 47 | + |
| 48 | +func TestParseV3WithMultileFiles(t *testing.T) { |
| 49 | + // set up files |
| 50 | + numOfServices := 3 |
| 51 | + fileString1 := `version: '3' |
| 52 | +services: |
| 53 | + wordpress: |
| 54 | + image: wordpress |
| 55 | + ports: ["80:80"] |
| 56 | + mysql: |
| 57 | + image: mysql` |
| 58 | + |
| 59 | + fileString2 := `version: '3' |
| 60 | +services: |
| 61 | + redis: |
| 62 | + image: redis |
| 63 | + ports: ["90:90"]` |
| 64 | + |
| 65 | + // initialize temp files |
| 66 | + tmpfile1, err1 := ioutil.TempFile("", "test") |
| 67 | + if err1 != nil { |
| 68 | + t.Fatal("Unexpected error in creating test file: ", err1) |
| 69 | + } |
| 70 | + defer os.Remove(tmpfile1.Name()) |
| 71 | + |
| 72 | + tmpfile2, err2 := ioutil.TempFile("", "test") |
| 73 | + if err2 != nil { |
| 74 | + t.Fatal("Unexpected error in creating test file: ", err2) |
| 75 | + } |
| 76 | + defer os.Remove(tmpfile2.Name()) |
| 77 | + |
| 78 | + // write compose contents to files |
| 79 | + if _, err := tmpfile1.Write([]byte(fileString1)); err != nil { |
| 80 | + t.Fatal("Unexpected error writing to test file: ", err) |
| 81 | + } |
| 82 | + if err := tmpfile1.Close(); err != nil { |
| 83 | + t.Fatal("Unexpected error closing test file: ", err) |
| 84 | + } |
| 85 | + |
| 86 | + if _, err := tmpfile2.Write([]byte(fileString2)); err != nil { |
| 87 | + t.Fatal("Unexpected error writing to test file: ", err) |
| 88 | + } |
| 89 | + if err := tmpfile2.Close(); err != nil { |
| 90 | + t.Fatal("Unexpected error closing test file: ", err) |
| 91 | + } |
| 92 | + // add files to projects |
| 93 | + project := setupTestProject(t) |
| 94 | + project.ecsContext.ComposeFiles = append(project.ecsContext.ComposeFiles, tmpfile1.Name()) |
| 95 | + project.ecsContext.ComposeFiles = append(project.ecsContext.ComposeFiles, tmpfile2.Name()) |
| 96 | + |
| 97 | + // assert number of container configs matches expected number of services |
| 98 | + // TODO: assert containerConfig content after conversion implemented |
| 99 | + conConfigResult, err := project.parseV3() |
| 100 | + if err != nil { |
| 101 | + t.Fatal("Unexpected error parsing file: ", err) |
| 102 | + } |
| 103 | + assert.Equal(t, numOfServices, len(*conConfigResult)) |
| 104 | +} |
0 commit comments