github.com/blixtra/rkt@v0.8.1-0.20160204105720-ab0d1add1a43/rkt/image/common_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 image
    16  
    17  import (
    18  	"io"
    19  	"io/ioutil"
    20  	"net/url"
    21  	"os"
    22  	"testing"
    23  	"time"
    24  
    25  	"github.com/coreos/rkt/common/apps"
    26  )
    27  
    28  func TestGuessImageType(t *testing.T) {
    29  	tests := []struct {
    30  		image        string
    31  		expectedType apps.AppImageType
    32  	}{
    33  		// guess obvious hash as a hash
    34  		{
    35  			image:        "sha512-a8d0943eb94eb9da4a6dddfa51e5e3de84375de77271d26c41ac1ce6f588b618",
    36  			expectedType: apps.AppImageHash,
    37  		},
    38  		// guess obvious URL as a URL
    39  		{
    40  			image:        "http://example.com/image.aci",
    41  			expectedType: apps.AppImageURL,
    42  		},
    43  		// guess obvious absolute path as a path
    44  		{
    45  			image:        "/usr/libexec/rkt/stage1.aci",
    46  			expectedType: apps.AppImagePath,
    47  		},
    48  		// guess stuff with colon as a name
    49  		{
    50  			image:        "example.com/stage1:1.2.3",
    51  			expectedType: apps.AppImageName,
    52  		},
    53  		// guess stuff with ./ as a path
    54  		{
    55  			image:        "some/relative/../path/with/dots/file",
    56  			expectedType: apps.AppImagePath,
    57  		},
    58  		// the same
    59  		{
    60  			image:        "./another/obviously/relative/path",
    61  			expectedType: apps.AppImagePath,
    62  		},
    63  		// guess stuff ending with .aci as a path
    64  		{
    65  			image:        "some/relative/path/with/aci/extension.aci",
    66  			expectedType: apps.AppImagePath,
    67  		},
    68  		// guess stuff without .aci, ./ and : as a name
    69  		{
    70  			image:        "example.com/stage1",
    71  			expectedType: apps.AppImageName,
    72  		},
    73  		// another try
    74  		{
    75  			image:        "example.com/stage1,version=1.2.3,foo=bar",
    76  			expectedType: apps.AppImageName,
    77  		},
    78  	}
    79  	for _, tt := range tests {
    80  		guessed := guessImageType(tt.image)
    81  		if tt.expectedType != guessed {
    82  			t.Errorf("expected %q to be guessed as %q, but got %q", tt.image, imageTypeToString(tt.expectedType), imageTypeToString(guessed))
    83  		}
    84  	}
    85  }
    86  
    87  func imageTypeToString(imType apps.AppImageType) string {
    88  	switch imType {
    89  	case apps.AppImageGuess:
    90  		return "to-be-guessed"
    91  	case apps.AppImageHash:
    92  		return "hash"
    93  	case apps.AppImageURL:
    94  		return "URL"
    95  	case apps.AppImagePath:
    96  		return "path"
    97  	case apps.AppImageName:
    98  		return "name"
    99  	default:
   100  		return "unknown"
   101  	}
   102  }
   103  
   104  func TestSignatureURLFromImageURL(t *testing.T) {
   105  	tests := []struct {
   106  		i string
   107  		s string
   108  	}{
   109  		{
   110  			i: "http://example.com/image",
   111  			s: "http://example.com/image.aci.asc",
   112  		},
   113  		{
   114  			i: "http://example.com/image.aci",
   115  			s: "http://example.com/image.aci.asc",
   116  		},
   117  		{
   118  			i: "http://example.com/image.aci?foo=bar&baz=quux#blah",
   119  			s: "http://example.com/image.aci.asc?foo=bar&baz=quux#blah",
   120  		},
   121  	}
   122  	for _, tt := range tests {
   123  		iu, err := url.Parse(tt.i)
   124  		if err != nil {
   125  			t.Errorf("failed to parse %q as an image URL: %v", tt.i, err)
   126  			continue
   127  		}
   128  		su, err := url.Parse(tt.s)
   129  		if err != nil {
   130  			t.Errorf("failed to parse %q as a signature URL: %v", tt.s, err)
   131  			continue
   132  		}
   133  		got := ascURLFromImgURL(iu)
   134  		if su.String() != got.String() {
   135  			t.Errorf("expected signature URL for image URL %q to be %q, but got %q", iu.String(), su.String(), got.String())
   136  		}
   137  	}
   138  }
   139  
   140  func TestSignaturePathFromImagePath(t *testing.T) {
   141  	tests := []struct {
   142  		i string
   143  		s string
   144  	}{
   145  		{
   146  			i: "/some/path/to/image",
   147  			s: "/some/path/to/image.aci.asc",
   148  		},
   149  		{
   150  			i: "/some/path/to/image.aci",
   151  			s: "/some/path/to/image.aci.asc",
   152  		},
   153  	}
   154  	for _, tt := range tests {
   155  		got := ascPathFromImgPath(tt.i)
   156  		if tt.s != got {
   157  			t.Errorf("expected signature path for image path %q to be %q, but got %q", tt.i, tt.s, got)
   158  		}
   159  	}
   160  }
   161  
   162  func TestUseCached(t *testing.T) {
   163  	tests := []struct {
   164  		age int
   165  		use bool
   166  	}{
   167  		{
   168  			age: -11,
   169  			use: false,
   170  		},
   171  		{
   172  			age: -1,
   173  			use: true,
   174  		},
   175  	}
   176  	maxAge := 10
   177  	for _, tt := range tests {
   178  		age := time.Now().Add(time.Duration(tt.age) * time.Second)
   179  		got := useCached(age, maxAge)
   180  		if got != tt.use {
   181  			t.Errorf("expected useCached(%v, %v) to return %v, but it returned %v", age, maxAge, tt.use, got)
   182  		}
   183  	}
   184  }
   185  
   186  func TestIsReallyNil(t *testing.T) {
   187  	tests := []struct {
   188  		name  string
   189  		iface interface{}
   190  		isNil bool
   191  	}{
   192  		// plain nil
   193  		{
   194  			name:  "plain nil",
   195  			iface: nil,
   196  			isNil: true,
   197  		},
   198  		// some pointer
   199  		{
   200  			name:  "some pointer",
   201  			iface: &struct{}{},
   202  			isNil: false,
   203  		},
   204  		// a nil interface
   205  		{
   206  			name:  "a nil interface",
   207  			iface: func() io.Closer { return nil }(),
   208  			isNil: true,
   209  		},
   210  		// a non-nil interface with nil value
   211  		{
   212  			name:  "a non-nil interface with nil value",
   213  			iface: func() io.Closer { var v *os.File; return v }(),
   214  			isNil: true,
   215  		},
   216  		// a non-nil interface with non-nil value
   217  		{
   218  			name:  "a non-nil interface with non-nil value",
   219  			iface: func() io.Closer { return ioutil.NopCloser(nil) }(),
   220  			isNil: false,
   221  		},
   222  	}
   223  	for _, tt := range tests {
   224  		t.Log(tt.name)
   225  		got := isReallyNil(tt.iface)
   226  		if tt.isNil != got {
   227  			t.Errorf("expected isReallyNil(%#v) to return %v, but got %v", tt.iface, tt.isNil, got)
   228  		}
   229  	}
   230  }