...

Source file src/github.com/google/go-containerregistry/pkg/v1/platform_test.go

Documentation: github.com/google/go-containerregistry/pkg/v1

     1  // Copyright 2020 Google LLC All Rights Reserved.
     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 v1_test
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/google/go-cmp/cmp"
    21  	v1 "github.com/google/go-containerregistry/pkg/v1"
    22  )
    23  
    24  func TestPlatformString(t *testing.T) {
    25  	for _, c := range []struct {
    26  		plat v1.Platform
    27  		want string
    28  	}{{
    29  		v1.Platform{},
    30  		"",
    31  	}, {
    32  		v1.Platform{OS: "linux"},
    33  		"linux",
    34  	}, {
    35  		v1.Platform{OS: "linux", Architecture: "amd64"},
    36  		"linux/amd64",
    37  	}, {
    38  		v1.Platform{OS: "linux", Architecture: "amd64", Variant: "v7"},
    39  		"linux/amd64/v7",
    40  	}, {
    41  		v1.Platform{OS: "linux", Architecture: "amd64", OSVersion: "1.2.3.4"},
    42  		"linux/amd64:1.2.3.4",
    43  	}, {
    44  		v1.Platform{OS: "linux", Architecture: "amd64", OSVersion: "1.2.3.4", OSFeatures: []string{"a", "b"}, Features: []string{"c", "d"}},
    45  		"linux/amd64:1.2.3.4",
    46  	}} {
    47  		if got := c.plat.String(); got != c.want {
    48  			t.Errorf("got %q, want %q", got, c.want)
    49  		}
    50  
    51  		if len(c.plat.OSFeatures) > 0 || len(c.plat.Features) > 0 {
    52  			// If these values are set, roundtripping back to the
    53  			// Platform will be lossy, and we expect that.
    54  			continue
    55  		}
    56  
    57  		back, err := v1.ParsePlatform(c.plat.String())
    58  		if err != nil {
    59  			t.Errorf("ParsePlatform(%q): %v", c.plat, err)
    60  		}
    61  		if d := cmp.Diff(&c.plat, back); d != "" {
    62  			t.Errorf("ParsePlatform(%q) diff:\n%s", c.plat.String(), d)
    63  		}
    64  	}
    65  
    66  	// Known bad examples.
    67  	for _, s := range []string{
    68  		"linux/amd64/v7/s9", // too many slashes
    69  	} {
    70  		got, err := v1.ParsePlatform(s)
    71  		if err == nil {
    72  			t.Errorf("ParsePlatform(%q) wanted error; got %v", s, got)
    73  		}
    74  	}
    75  }
    76  
    77  func TestPlatformEquals(t *testing.T) {
    78  	tests := []struct {
    79  		a, b  v1.Platform
    80  		equal bool
    81  	}{{
    82  		v1.Platform{Architecture: "amd64", OS: "linux"},
    83  		v1.Platform{Architecture: "amd64", OS: "linux"},
    84  		true,
    85  	}, {
    86  		v1.Platform{Architecture: "amd64", OS: "linux"},
    87  		v1.Platform{Architecture: "arm64", OS: "linux"},
    88  		false,
    89  	}, {
    90  		v1.Platform{Architecture: "amd64", OS: "linux"},
    91  		v1.Platform{Architecture: "amd64", OS: "darwin"},
    92  		false,
    93  	}, {
    94  		v1.Platform{Architecture: "amd64", OS: "linux", OSVersion: "5.0"},
    95  		v1.Platform{Architecture: "amd64", OS: "linux"},
    96  		false,
    97  	}, {
    98  		v1.Platform{Architecture: "amd64", OS: "linux", OSVersion: "5.0"},
    99  		v1.Platform{Architecture: "amd64", OS: "linux", OSVersion: "3.6"},
   100  		false,
   101  	}, {
   102  		v1.Platform{Architecture: "amd64", OS: "linux", Variant: "pios"},
   103  		v1.Platform{Architecture: "amd64", OS: "linux"},
   104  		false,
   105  	}, {
   106  		v1.Platform{Architecture: "amd64", OS: "linux", Variant: "pios"},
   107  		v1.Platform{Architecture: "amd64", OS: "linux", Variant: "ubuntu"},
   108  		false,
   109  	}, {
   110  		v1.Platform{Architecture: "amd64", OS: "linux", Variant: "pios"},
   111  		v1.Platform{Architecture: "amd64", OS: "linux", Variant: "pios"},
   112  		true,
   113  	}, {
   114  		v1.Platform{Architecture: "amd64", OS: "linux", OSFeatures: []string{"a", "b"}},
   115  		v1.Platform{Architecture: "amd64", OS: "linux"},
   116  		false,
   117  	}, {
   118  		v1.Platform{Architecture: "amd64", OS: "linux", OSFeatures: []string{"a", "b"}},
   119  		v1.Platform{Architecture: "amd64", OS: "linux", OSFeatures: []string{"a", "b"}},
   120  		true,
   121  	}, {
   122  		v1.Platform{Architecture: "amd64", OS: "linux", OSFeatures: []string{"a", "b"}},
   123  		v1.Platform{Architecture: "amd64", OS: "linux", OSFeatures: []string{"ac", "bd"}},
   124  		false,
   125  	}, {
   126  		v1.Platform{Architecture: "amd64", OS: "linux", OSFeatures: []string{"a", "b"}},
   127  		v1.Platform{Architecture: "amd64", OS: "linux", OSFeatures: []string{"b", "a"}},
   128  		true,
   129  	}, {
   130  		v1.Platform{Architecture: "amd64", OS: "linux", Features: []string{"a", "b"}},
   131  		v1.Platform{Architecture: "amd64", OS: "linux"},
   132  		false,
   133  	}, {
   134  		v1.Platform{Architecture: "amd64", OS: "linux", Features: []string{"a", "b"}},
   135  		v1.Platform{Architecture: "amd64", OS: "linux", Features: []string{"a", "b"}},
   136  		true,
   137  	}, {
   138  		v1.Platform{Architecture: "amd64", OS: "linux", Features: []string{"a", "b"}},
   139  		v1.Platform{Architecture: "amd64", OS: "linux", Features: []string{"ac", "bd"}},
   140  		false,
   141  	}, {
   142  		v1.Platform{Architecture: "amd64", OS: "linux", Features: []string{"a", "b"}},
   143  		v1.Platform{Architecture: "amd64", OS: "linux", Features: []string{"b", "a"}},
   144  		true,
   145  	}}
   146  	for i, tt := range tests {
   147  		if equal := tt.a.Equals(tt.b); equal != tt.equal {
   148  			t.Errorf("%d: mismatched was %v expected %v; original (-want +got) %s", i, equal, tt.equal, cmp.Diff(tt.a, tt.b))
   149  		}
   150  	}
   151  }
   152  
   153  func TestPlatformSatisfies(t *testing.T) {
   154  	tests := []struct {
   155  		have, spec v1.Platform
   156  		sat        bool
   157  	}{{
   158  		v1.Platform{Architecture: "amd64", OS: "linux"},
   159  		v1.Platform{Architecture: "amd64", OS: "linux"},
   160  		true,
   161  	}, {
   162  		v1.Platform{Architecture: "amd64", OS: "linux"},
   163  		v1.Platform{Architecture: "arm64", OS: "linux"},
   164  		false,
   165  	}, {
   166  		v1.Platform{Architecture: "amd64", OS: "linux"},
   167  		v1.Platform{Architecture: "amd64", OS: "darwin"},
   168  		false,
   169  	}, {
   170  		v1.Platform{Architecture: "amd64", OS: "linux", OSVersion: "5.0"},
   171  		v1.Platform{Architecture: "amd64", OS: "linux"},
   172  		true,
   173  	}, {
   174  		v1.Platform{Architecture: "amd64", OS: "linux", OSVersion: "5.0"},
   175  		v1.Platform{Architecture: "amd64", OS: "linux", OSVersion: "3.6"},
   176  		false,
   177  	}, {
   178  		v1.Platform{Architecture: "amd64", OS: "linux", Variant: "pios"},
   179  		v1.Platform{Architecture: "amd64", OS: "linux"},
   180  		true,
   181  	}, {
   182  		v1.Platform{Architecture: "amd64", OS: "linux", Variant: "pios"},
   183  		v1.Platform{Architecture: "amd64", OS: "linux", Variant: "ubuntu"},
   184  		false,
   185  	}, {
   186  		v1.Platform{Architecture: "amd64", OS: "linux", Variant: "pios"},
   187  		v1.Platform{Architecture: "amd64", OS: "linux", Variant: "pios"},
   188  		true,
   189  	}, {
   190  		v1.Platform{Architecture: "amd64", OS: "linux"},
   191  		v1.Platform{Architecture: "amd64", OS: "linux", OSFeatures: []string{"a", "b"}},
   192  		false,
   193  	}, {
   194  		v1.Platform{Architecture: "amd64", OS: "linux", OSFeatures: []string{"a", "b"}},
   195  		v1.Platform{Architecture: "amd64", OS: "linux"},
   196  		true,
   197  	}, {
   198  		v1.Platform{Architecture: "amd64", OS: "linux", OSFeatures: []string{"a", "b"}},
   199  		v1.Platform{Architecture: "amd64", OS: "linux", OSFeatures: []string{"a", "b"}},
   200  		true,
   201  	}, {
   202  		v1.Platform{Architecture: "amd64", OS: "linux", OSFeatures: []string{"a", "b"}},
   203  		v1.Platform{Architecture: "amd64", OS: "linux", OSFeatures: []string{"ac", "bd"}},
   204  		false,
   205  	}, {
   206  		v1.Platform{Architecture: "amd64", OS: "linux", OSFeatures: []string{"a", "b"}},
   207  		v1.Platform{Architecture: "amd64", OS: "linux", OSFeatures: []string{"b", "a"}},
   208  		true,
   209  	}, {
   210  		v1.Platform{Architecture: "amd64", OS: "linux"},
   211  		v1.Platform{Architecture: "amd64", OS: "linux", Features: []string{"a", "b"}},
   212  		false,
   213  	}, {
   214  		v1.Platform{Architecture: "amd64", OS: "linux", Features: []string{"a", "b"}},
   215  		v1.Platform{Architecture: "amd64", OS: "linux"},
   216  		true,
   217  	}, {
   218  		v1.Platform{Architecture: "amd64", OS: "linux", Features: []string{"a", "b"}},
   219  		v1.Platform{Architecture: "amd64", OS: "linux", Features: []string{"a", "b"}},
   220  		true,
   221  	}, {
   222  		v1.Platform{Architecture: "amd64", OS: "linux", Features: []string{"a", "b"}},
   223  		v1.Platform{Architecture: "amd64", OS: "linux", Features: []string{"ac", "bd"}},
   224  		false,
   225  	}, {
   226  		v1.Platform{Architecture: "amd64", OS: "linux", Features: []string{"a", "b"}},
   227  		v1.Platform{Architecture: "amd64", OS: "linux", Features: []string{"b", "a"}},
   228  		true,
   229  	}}
   230  	for i, tt := range tests {
   231  		if sat := tt.have.Satisfies(tt.spec); sat != tt.sat {
   232  			t.Errorf("%d: mismatched was %v expected %v; original (-want +got) %s", i, sat, tt.sat, cmp.Diff(tt.have, tt.spec))
   233  		}
   234  	}
   235  }
   236  

View as plain text