github.com/stackdocker/rkt@v0.10.1-0.20151109095037-1aa827478248/tests/rkt_os_arch_test.go (about)

     1  // Copyright 2015 The rkt Authors
     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 main
    16  
    17  import (
    18  	"fmt"
    19  	"io/ioutil"
    20  	"os"
    21  	"strings"
    22  	"testing"
    23  
    24  	"github.com/coreos/rkt/tests/testutils"
    25  )
    26  
    27  const (
    28  	manifestOSArchTemplate = `{"acKind":"ImageManifest","acVersion":"0.7.1","name":"IMG_NAME","labels":[{"name":"version","value":"1.0.0"}ARCH_OS],"app":{"exec":["/inspect", "--print-msg=HelloWorld"],"user":"0","group":"0","workingDirectory":"/"}}`
    29  )
    30  
    31  type osArchTest struct {
    32  	image        string
    33  	rktCmd       string
    34  	expectedLine string
    35  	expectError  bool
    36  }
    37  
    38  func osArchTestRemoveImages(tests []osArchTest) {
    39  	for _, tt := range tests {
    40  		os.Remove(tt.image)
    41  	}
    42  }
    43  
    44  func getMissingOrInvalidTests(t *testing.T, ctx *testutils.RktRunCtx) []osArchTest {
    45  	var tests []osArchTest
    46  
    47  	defer osArchTestRemoveImages(tests)
    48  	testImageName := "coreos.com/rkt-missing-os-arch-test"
    49  	manifest := strings.Replace(manifestOSArchTemplate, "IMG_NAME", testImageName, 1)
    50  
    51  	// Test a valid image as a sanity check
    52  	goodManifestStr := strings.Replace(manifest, "ARCH_OS", `,{"name":"os","value":"linux"},{"name":"arch","value":"amd64"}`, 1)
    53  	goodManifestFile := "good-manifest.json"
    54  	if err := ioutil.WriteFile(goodManifestFile, []byte(goodManifestStr), 0600); err != nil {
    55  		t.Fatalf("Cannot write good manifest: %v", err)
    56  	}
    57  	defer os.Remove(goodManifestFile)
    58  
    59  	goodImage := patchTestACI("rkt-good-image.aci", fmt.Sprintf("--manifest=%s", goodManifestFile))
    60  	goodTest := osArchTest{
    61  		image:        goodImage,
    62  		rktCmd:       fmt.Sprintf("%s --insecure-skip-verify run --mds-register=false %s", ctx.Cmd(), goodImage),
    63  		expectedLine: "HelloWorld",
    64  		expectError:  false,
    65  	}
    66  	tests = append(tests, goodTest)
    67  
    68  	// Test an image with a missing os label
    69  	missingOSManifestStr := strings.Replace(manifest, "ARCH_OS", `,{"name":"arch","value":"amd64"}`, 1)
    70  	missingOSManifestFile := "missingOS-manifest.json"
    71  	if err := ioutil.WriteFile(missingOSManifestFile, []byte(missingOSManifestStr), 0600); err != nil {
    72  		t.Fatalf("Cannot write missing OS manifest: %v", err)
    73  	}
    74  	defer os.Remove(missingOSManifestFile)
    75  
    76  	missingOSImage := patchTestACI("rkt-missing-os.aci", fmt.Sprintf("--manifest=%s", missingOSManifestFile))
    77  	missingOSTest := osArchTest{
    78  		image:        missingOSImage,
    79  		rktCmd:       fmt.Sprintf("%s --insecure-skip-verify run %s", ctx.Cmd(), missingOSImage),
    80  		expectedLine: "missing os label in the image manifest",
    81  		expectError:  true,
    82  	}
    83  	tests = append(tests, missingOSTest)
    84  
    85  	// Test an image with a missing arch label
    86  	missingArchManifestStr := strings.Replace(manifest, "ARCH_OS", `,{"name":"os","value":"linux"}`, 1)
    87  	missingArchManifestFile := "missingArch-manifest.json"
    88  	if err := ioutil.WriteFile(missingArchManifestFile, []byte(missingArchManifestStr), 0600); err != nil {
    89  		t.Fatalf("Cannot write missing Arch manifest: %v", err)
    90  	}
    91  	defer os.Remove(missingArchManifestFile)
    92  
    93  	missingArchImage := patchTestACI("rkt-missing-arch.aci", fmt.Sprintf("--manifest=%s", missingArchManifestFile))
    94  	missingArchTest := osArchTest{
    95  		image:        missingArchImage,
    96  		rktCmd:       fmt.Sprintf("%s --insecure-skip-verify run %s", ctx.Cmd(), missingArchImage),
    97  		expectedLine: "missing arch label in the image manifest",
    98  		expectError:  true,
    99  	}
   100  	tests = append(tests, missingArchTest)
   101  
   102  	// Test an image with an invalid os
   103  	invalidOSManifestStr := strings.Replace(manifest, "ARCH_OS", `,{"name":"os","value":"freebsd"},{"name":"arch","value":"amd64"}`, 1)
   104  	invalidOSManifestFile := "invalid-os-manifest.json"
   105  	if err := ioutil.WriteFile(invalidOSManifestFile, []byte(invalidOSManifestStr), 0600); err != nil {
   106  		t.Fatalf("Cannot write invalid os manifest: %v", err)
   107  	}
   108  	defer os.Remove(invalidOSManifestFile)
   109  
   110  	invalidOSImage := patchTestACI("rkt-invalid-os.aci", fmt.Sprintf("--manifest=%s", invalidOSManifestFile))
   111  	invalidOSTest := osArchTest{
   112  		image:        invalidOSImage,
   113  		rktCmd:       fmt.Sprintf("%s --insecure-skip-verify run %s", ctx.Cmd(), invalidOSImage),
   114  		expectedLine: `bad os "freebsd"`,
   115  		expectError:  true,
   116  	}
   117  	tests = append(tests, invalidOSTest)
   118  
   119  	// Test an image with an invalid arch
   120  	invalidArchManifestStr := strings.Replace(manifest, "ARCH_OS", `,{"name":"os","value":"linux"},{"name":"arch","value":"armv6l"}`, 1)
   121  	invalidArchManifestFile := "invalid-arch-manifest.json"
   122  	if err := ioutil.WriteFile(invalidArchManifestFile, []byte(invalidArchManifestStr), 0600); err != nil {
   123  		t.Fatalf("Cannot write invalid arch manifest: %v", err)
   124  	}
   125  	defer os.Remove(invalidArchManifestFile)
   126  
   127  	invalidArchImage := patchTestACI("rkt-invalid-arch.aci", fmt.Sprintf("--manifest=%s", invalidArchManifestFile))
   128  	invalidArchTest := osArchTest{
   129  		image:        invalidArchImage,
   130  		rktCmd:       fmt.Sprintf("%s --insecure-skip-verify run %s", ctx.Cmd(), invalidArchImage),
   131  		expectedLine: `bad arch "armv6l"`,
   132  		expectError:  true,
   133  	}
   134  	tests = append(tests, invalidArchTest)
   135  
   136  	retTests := tests
   137  	tests = nil
   138  	return retTests
   139  }
   140  
   141  // TestMissingOrInvalidOSArchRun tests that rkt errors out when it tries to run
   142  // an image (not present in the store) with a missing or unsupported os/arch
   143  func TestMissingOrInvalidOSArchRun(t *testing.T) {
   144  	ctx := testutils.NewRktRunCtx()
   145  	defer ctx.Cleanup()
   146  	tests := getMissingOrInvalidTests(t, ctx)
   147  	defer osArchTestRemoveImages(tests)
   148  
   149  	for i, tt := range tests {
   150  		t.Logf("Running test #%v: %v", i, tt.rktCmd)
   151  		runRktAndCheckOutput(t, tt.rktCmd, tt.expectedLine, tt.expectError)
   152  	}
   153  }
   154  
   155  // TestMissingOrInvalidOSArchFetchRun tests that rkt errors out when it tries
   156  // to run an already fetched image with a missing or unsupported os/arch
   157  func TestMissingOrInvalidOSArchFetchRun(t *testing.T) {
   158  	ctx := testutils.NewRktRunCtx()
   159  	defer ctx.Cleanup()
   160  	tests := getMissingOrInvalidTests(t, ctx)
   161  	defer osArchTestRemoveImages(tests)
   162  
   163  	for i, tt := range tests {
   164  		imgHash := importImageAndFetchHash(t, ctx, tt.image)
   165  		rktCmd := fmt.Sprintf("%s run --mds-register=false %s", ctx.Cmd(), imgHash)
   166  		t.Logf("Running test #%v: %v", i, rktCmd)
   167  		runRktAndCheckOutput(t, rktCmd, tt.expectedLine, tt.expectError)
   168  	}
   169  }