github.com/coreos/mantle@v0.13.0/kola/tests/docker/torcx_manifest_pkgs.go (about)

     1  // Copyright 2017 CoreOS, Inc.
     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 docker
    16  
    17  import (
    18  	"encoding/json"
    19  	"fmt"
    20  	"strings"
    21  
    22  	ignition "github.com/coreos/ignition/config/v2_1/types"
    23  	"github.com/coreos/mantle/kola"
    24  	"github.com/coreos/mantle/kola/cluster"
    25  	"github.com/coreos/mantle/kola/register"
    26  	"github.com/coreos/mantle/kola/torcx"
    27  	"github.com/coreos/mantle/platform"
    28  	"github.com/coreos/mantle/platform/conf"
    29  )
    30  
    31  func init() {
    32  	register.Register(&register.Test{
    33  		Run:         dockerTorcxManifestPkgs,
    34  		ClusterSize: 0,
    35  		Name:        "docker.torcx-manifest-pkgs",
    36  		Flags:       []register.Flag{register.RequiresInternetAccess}, // Downloads torcx packages
    37  		// https://github.com/coreos/bugs/issues/2205 for DO
    38  		ExcludePlatforms: []string{"do"},
    39  		Distros:          []string{"cl"},
    40  	})
    41  }
    42  
    43  func dockerTorcxManifestPkgs(c cluster.TestCluster) {
    44  	if kola.TorcxManifest == nil {
    45  		c.Skip("no torcx manifest provided")
    46  		return
    47  	}
    48  
    49  	var dockerPkgs *torcx.Package
    50  	for _, pkg := range kola.TorcxManifest.Packages {
    51  		pkg := pkg
    52  		if pkg.Name == "docker" {
    53  			dockerPkgs = &pkg
    54  			break
    55  		}
    56  	}
    57  	if dockerPkgs == nil {
    58  		c.Fatalf("torcx manifest provided, but didn't include docker packages: %+v", kola.TorcxManifest)
    59  	}
    60  
    61  	// Generate an ignition config that downloads all of the docker torcx packages referenced
    62  	ignitionConfig := ignition.Config{
    63  		Ignition: ignition.Ignition{
    64  			Version: "2.1.0",
    65  		},
    66  		Storage: ignition.Storage{
    67  			Files: []ignition.File{},
    68  		},
    69  	}
    70  
    71  	for _, version := range dockerPkgs.Versions {
    72  		version := version
    73  		var url string
    74  		for _, loc := range version.Locations {
    75  			if loc.URL != nil {
    76  				url = *loc.URL
    77  			}
    78  		}
    79  		if url == "" {
    80  			c.Fatalf("not all docker versions had a remote location available: %+v", kola.TorcxManifest)
    81  		}
    82  
    83  		ignitionConfig.Storage.Files = append(ignitionConfig.Storage.Files, ignition.File{
    84  			Node: ignition.Node{
    85  				Filesystem: "root",
    86  				Path:       fmt.Sprintf("/var/lib/torcx/store/docker:%s.torcx.tgz", version.Version),
    87  			},
    88  			FileEmbedded1: ignition.FileEmbedded1{
    89  				Contents: ignition.FileContents{
    90  					Source: url,
    91  					Verification: ignition.Verification{
    92  						Hash: &version.Hash,
    93  					},
    94  				},
    95  				Mode: 0644,
    96  			},
    97  		})
    98  	}
    99  
   100  	ignitionBytes, err := json.Marshal(ignitionConfig)
   101  	if err != nil {
   102  		c.Fatalf("marshal err: %v", err)
   103  	}
   104  
   105  	m, err := c.NewMachine(conf.Ignition(string(ignitionBytes)))
   106  	if err != nil {
   107  		c.Fatalf("could not boot machine: %v", err)
   108  	}
   109  
   110  	// Make sure the default torcx config was fine
   111  	c.MustSSH(m, `docker version`)
   112  
   113  	// And now swap in a profile for each package and make sure it works
   114  	for _, version := range dockerPkgs.Versions {
   115  		version := version.Version
   116  		c.Run("torcx-pkg-"+version, func(c cluster.TestCluster) {
   117  			testPackageVersion(m, c, version)
   118  		})
   119  	}
   120  }
   121  
   122  func testPackageVersion(m platform.Machine, c cluster.TestCluster, version string) {
   123  	c.Run("install-torcx-profile", func(c cluster.TestCluster) {
   124  		c.MustSSH(m, fmt.Sprintf(`sudo tee /etc/torcx/profiles/docker.json <<EOF
   125  {
   126    "kind": "profile-manifest-v0",
   127    "value": {
   128      "images": [
   129        {
   130          "name": "docker",
   131          "reference": "%s"
   132        }
   133      ]
   134    }
   135  }
   136  EOF
   137  echo "docker" | sudo tee /etc/torcx/next-profile
   138  `, version))
   139  
   140  		if err := m.Reboot(); err != nil {
   141  			c.Fatalf("could not reboot: %v", err)
   142  		}
   143  		c.MustSSH(m, `sudo rm -rf /var/lib/docker`)
   144  		currentVersion := getTorcxDockerReference(c, m)
   145  		if currentVersion != version {
   146  			c.Fatalf("expected version to be %s, was %s", version, currentVersion)
   147  		}
   148  
   149  		serverVersion := getDockerServerVersion(c, m)
   150  		// torcx packages have truncated docker versions, e.g. 1.12.6 has a torcx
   151  		// package of 1.12
   152  		if !strings.HasPrefix(serverVersion, version) {
   153  			c.Fatalf("expected a version similar to %v, was %v", version, serverVersion)
   154  		}
   155  
   156  	})
   157  
   158  	dockerBaseTests(c)
   159  }
   160  
   161  func getTorcxDockerReference(c cluster.TestCluster, m platform.Machine) string {
   162  	ver := c.MustSSH(m, `jq -r '.value.images[] | select(.name == "docker").reference' /run/torcx/profile.json`)
   163  	return string(ver)
   164  }
   165  
   166  func getDockerServerVersion(c cluster.TestCluster, m platform.Machine) string {
   167  	ver := c.MustSSH(m, `curl -s --unix-socket /var/run/docker.sock http://docker/v1.24/info | jq -r '.ServerVersion'`)
   168  	return string(ver)
   169  }