github.com/containerd/nerdctl@v1.7.7/cmd/nerdctl/compose_images_linux_test.go (about) 1 /* 2 Copyright The containerd Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package main 18 19 import ( 20 "encoding/json" 21 "fmt" 22 "strings" 23 "testing" 24 25 "github.com/containerd/nerdctl/pkg/testutil" 26 ) 27 28 func TestComposeImages(t *testing.T) { 29 base := testutil.NewBase(t) 30 var dockerComposeYAML = fmt.Sprintf(` 31 version: '3.1' 32 33 services: 34 wordpress: 35 image: %s 36 ports: 37 - 8080:80 38 environment: 39 WORDPRESS_DB_HOST: db 40 WORDPRESS_DB_USER: exampleuser 41 WORDPRESS_DB_PASSWORD: examplepass 42 WORDPRESS_DB_NAME: exampledb 43 volumes: 44 - wordpress:/var/www/html 45 db: 46 image: %s 47 environment: 48 MYSQL_DATABASE: exampledb 49 MYSQL_USER: exampleuser 50 MYSQL_PASSWORD: examplepass 51 MYSQL_RANDOM_ROOT_PASSWORD: '1' 52 volumes: 53 - db:/var/lib/mysql 54 55 volumes: 56 wordpress: 57 db: 58 `, testutil.WordpressImage, testutil.MariaDBImage) 59 60 comp := testutil.NewComposeDir(t, dockerComposeYAML) 61 defer comp.CleanUp() 62 projectName := comp.ProjectName() 63 t.Logf("projectName=%q", projectName) 64 65 base.ComposeCmd("-f", comp.YAMLFullPath(), "up", "-d").AssertOK() 66 defer base.ComposeCmd("-f", comp.YAMLFullPath(), "down", "-v").Run() 67 68 wordpressImageName := strings.Split(testutil.WordpressImage, ":")[0] 69 dbImageName := strings.Split(testutil.MariaDBImage, ":")[0] 70 71 // check one service image 72 base.ComposeCmd("-f", comp.YAMLFullPath(), "images", "db").AssertOutContains(dbImageName) 73 base.ComposeCmd("-f", comp.YAMLFullPath(), "images", "db").AssertOutNotContains(wordpressImageName) 74 75 // check all service images 76 base.ComposeCmd("-f", comp.YAMLFullPath(), "images").AssertOutContains(dbImageName) 77 base.ComposeCmd("-f", comp.YAMLFullPath(), "images").AssertOutContains(wordpressImageName) 78 } 79 80 func TestComposeImagesJson(t *testing.T) { 81 base := testutil.NewBase(t) 82 var dockerComposeYAML = fmt.Sprintf(` 83 version: '3.1' 84 85 services: 86 wordpress: 87 image: %s 88 container_name: wordpress 89 ports: 90 - 8080:80 91 environment: 92 WORDPRESS_DB_HOST: db 93 WORDPRESS_DB_USER: exampleuser 94 WORDPRESS_DB_PASSWORD: examplepass 95 WORDPRESS_DB_NAME: exampledb 96 volumes: 97 - wordpress:/var/www/html 98 db: 99 image: %s 100 container_name: db 101 environment: 102 MYSQL_DATABASE: exampledb 103 MYSQL_USER: exampleuser 104 MYSQL_PASSWORD: examplepass 105 MYSQL_RANDOM_ROOT_PASSWORD: '1' 106 volumes: 107 - db:/var/lib/mysql 108 109 volumes: 110 wordpress: 111 db: 112 `, testutil.WordpressImage, testutil.MariaDBImage) 113 114 comp := testutil.NewComposeDir(t, dockerComposeYAML) 115 defer comp.CleanUp() 116 projectName := comp.ProjectName() 117 t.Logf("projectName=%q", projectName) 118 119 base.ComposeCmd("-f", comp.YAMLFullPath(), "up", "-d").AssertOK() 120 defer base.ComposeCmd("-f", comp.YAMLFullPath(), "down", "-v").Run() 121 122 assertHandler := func(svc string, count int, fields ...string) func(stdout string) error { 123 return func(stdout string) error { 124 // 1. check json output can be unmarshalled back to printables. 125 var printables []composeContainerPrintable 126 if err := json.Unmarshal([]byte(stdout), &printables); err != nil { 127 return fmt.Errorf("[service: %s]failed to unmarshal json output from `compose images`: %s", svc, stdout) 128 } 129 // 2. check #printables matches expected count. 130 if len(printables) != count { 131 return fmt.Errorf("[service: %s]unmarshal generates %d printables, expected %d: %s", svc, len(printables), count, stdout) 132 } 133 // 3. check marshalled json string has all expected substrings. 134 for _, field := range fields { 135 if !strings.Contains(stdout, field) { 136 return fmt.Errorf("[service: %s]marshalled json output doesn't have expected string (%s): %s", svc, field, stdout) 137 } 138 } 139 return nil 140 } 141 } 142 143 // check other formats are not supported 144 base.ComposeCmd("-f", comp.YAMLFullPath(), "images", "--format", "yaml").AssertFail() 145 // check all services are up (can be marshalled and unmarshalled) 146 base.ComposeCmd("-f", comp.YAMLFullPath(), "images", "--format", "json"). 147 AssertOutWithFunc(assertHandler("all", 2, `"ContainerName":"wordpress"`, `"ContainerName":"db"`)) 148 149 base.ComposeCmd("-f", comp.YAMLFullPath(), "images", "--format", "json", "wordpress"). 150 AssertOutWithFunc(assertHandler("wordpress", 1, `"ContainerName":"wordpress"`)) 151 }