github.com/containerd/nerdctl@v1.7.7/cmd/nerdctl/ipfs_compose_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  	"fmt"
    21  	"io"
    22  	"os"
    23  	"strings"
    24  	"testing"
    25  
    26  	"github.com/containerd/nerdctl/pkg/testutil"
    27  	"github.com/containerd/nerdctl/pkg/testutil/nettestutil"
    28  	"gotest.tools/v3/assert"
    29  )
    30  
    31  func TestIPFSComposeUp(t *testing.T) {
    32  	testutil.DockerIncompatible(t)
    33  	base := testutil.NewBase(t)
    34  	ipfsaddr, done := runIPFSDaemonContainer(t, base)
    35  	defer done()
    36  	tests := []struct {
    37  		name           string
    38  		snapshotter    string
    39  		pushOptions    []string
    40  		composeOptions []string
    41  		requiresStargz bool
    42  	}{
    43  		{
    44  			name:        "overlayfs",
    45  			snapshotter: "overlayfs",
    46  		},
    47  		{
    48  			name:           "stargz",
    49  			snapshotter:    "stargz",
    50  			pushOptions:    []string{"--estargz"},
    51  			requiresStargz: true,
    52  		},
    53  		{
    54  			name:           "ipfs-address",
    55  			snapshotter:    "overlayfs",
    56  			pushOptions:    []string{fmt.Sprintf("--ipfs-address=%s", ipfsaddr)},
    57  			composeOptions: []string{fmt.Sprintf("--ipfs-address=%s", ipfsaddr)},
    58  		},
    59  	}
    60  	for _, tt := range tests {
    61  		t.Run(tt.name, func(t *testing.T) {
    62  			base := testutil.NewBase(t)
    63  			if tt.requiresStargz {
    64  				requiresStargz(base)
    65  			}
    66  			ipfsImgs := make([]string, 2)
    67  			for i, img := range []string{testutil.WordpressImage, testutil.MariaDBImage} {
    68  				ipfsImgs[i] = pushImageToIPFS(t, base, img, tt.pushOptions...)
    69  			}
    70  			base.Env = append(os.Environ(), "CONTAINERD_SNAPSHOTTER="+tt.snapshotter)
    71  			testComposeUp(t, base, fmt.Sprintf(`
    72  version: '3.1'
    73  
    74  services:
    75  
    76    wordpress:
    77      image: %s
    78      restart: always
    79      ports:
    80        - 8080:80
    81      environment:
    82        WORDPRESS_DB_HOST: db
    83        WORDPRESS_DB_USER: exampleuser
    84        WORDPRESS_DB_PASSWORD: examplepass
    85        WORDPRESS_DB_NAME: exampledb
    86      volumes:
    87        # workaround for https://github.com/containerd/stargz-snapshotter/issues/444
    88        - "/run"
    89        - wordpress:/var/www/html
    90  
    91    db:
    92      image: %s
    93      restart: always
    94      environment:
    95        MYSQL_DATABASE: exampledb
    96        MYSQL_USER: exampleuser
    97        MYSQL_PASSWORD: examplepass
    98        MYSQL_RANDOM_ROOT_PASSWORD: '1'
    99      volumes:
   100        # workaround for https://github.com/containerd/stargz-snapshotter/issues/444
   101        - "/run"
   102        - db:/var/lib/mysql
   103  
   104  volumes:
   105    wordpress:
   106    db:
   107  `, ipfsImgs[0], ipfsImgs[1]), tt.composeOptions...)
   108  		})
   109  	}
   110  }
   111  
   112  func TestIPFSComposeUpBuild(t *testing.T) {
   113  	testutil.DockerIncompatible(t)
   114  	testutil.RequiresBuild(t)
   115  	base := testutil.NewBase(t)
   116  	defer base.Cmd("builder", "prune").Run()
   117  	ipfsCID := pushImageToIPFS(t, base, testutil.NginxAlpineImage)
   118  	ipfsCIDBase := strings.TrimPrefix(ipfsCID, "ipfs://")
   119  
   120  	const dockerComposeYAML = `
   121  services:
   122    web:
   123      build: .
   124      ports:
   125      - 8080:80
   126  `
   127  	dockerfile := fmt.Sprintf(`FROM localhost:5050/ipfs/%s
   128  COPY index.html /usr/share/nginx/html/index.html
   129  `, ipfsCIDBase)
   130  	indexHTML := t.Name()
   131  
   132  	comp := testutil.NewComposeDir(t, dockerComposeYAML)
   133  	defer comp.CleanUp()
   134  
   135  	comp.WriteFile("Dockerfile", dockerfile)
   136  	comp.WriteFile("index.html", indexHTML)
   137  
   138  	done := ipfsRegistryUp(t, base)
   139  	defer done()
   140  	base.ComposeCmd("-f", comp.YAMLFullPath(), "up", "-d", "--build").AssertOK()
   141  	defer base.ComposeCmd("-f", comp.YAMLFullPath(), "down", "-v").Run()
   142  
   143  	resp, err := nettestutil.HTTPGet("http://127.0.0.1:8080", 50, false)
   144  	assert.NilError(t, err)
   145  	respBody, err := io.ReadAll(resp.Body)
   146  	assert.NilError(t, err)
   147  	t.Logf("respBody=%q", respBody)
   148  	assert.Assert(t, strings.Contains(string(respBody), indexHTML))
   149  }