github.com/rkt/rkt@v1.30.1-0.20200224141603-171c416fac02/pkg/distribution/appc_test.go (about)

     1  // Copyright 2016 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 distribution
    16  
    17  import (
    18  	"net/url"
    19  	"reflect"
    20  	"testing"
    21  
    22  	"github.com/appc/spec/discovery"
    23  )
    24  
    25  func TestNewAppcFromAppString(t *testing.T) {
    26  	tests := []struct {
    27  		appcRef  string
    28  		expected string
    29  	}{
    30  		{
    31  			"example.com/app01",
    32  			"cimd:appc:v=0:example.com/app01",
    33  		},
    34  		{
    35  			"example.com/app01:v1.0.0",
    36  			"cimd:appc:v=0:example.com/app01?version=v1.0.0",
    37  		},
    38  		{
    39  			"example.com/app01,version=v1.0.0",
    40  			"cimd:appc:v=0:example.com/app01?version=v1.0.0",
    41  		},
    42  		{
    43  			"example.com/app01,version=v1.0.0,label01=?&*/",
    44  			"cimd:appc:v=0:example.com/app01?label01=%3F%26%2A%2F&version=v1.0.0",
    45  		},
    46  		{
    47  			"example-app01",
    48  			"cimd:appc:v=0:example-app01",
    49  		},
    50  		{
    51  			"example-app01:v1.0",
    52  			"cimd:appc:v=0:example-app01?version=v1.0",
    53  		},
    54  	}
    55  
    56  	for _, tt := range tests {
    57  		app, err := discovery.NewAppFromString(tt.appcRef)
    58  		if err != nil {
    59  			t.Fatalf("unexpected error: %v", err)
    60  		}
    61  		appc := NewAppcFromApp(app)
    62  		if err != nil {
    63  			t.Fatalf("unexpected error: %v", err)
    64  		}
    65  
    66  		u, err := url.Parse(tt.expected)
    67  		if err != nil {
    68  			t.Fatalf("unexpected error: %v", err)
    69  		}
    70  		td, err := NewAppc(u)
    71  		if err != nil {
    72  			t.Fatalf("unexpected error: %v", err)
    73  		}
    74  		if !appc.Equals(td) {
    75  			t.Errorf("expected identical distribution but got %q != %q", td.CIMD().String(), appc.CIMD().String())
    76  			continue
    77  		}
    78  	}
    79  }
    80  
    81  func TestApp(t *testing.T) {
    82  	tests := []struct {
    83  		uriStr string
    84  		out    string
    85  	}{
    86  		{
    87  			"cimd:appc:v=0:example.com/app01",
    88  			"example.com/app01",
    89  		},
    90  		{
    91  			"cimd:appc:v=0:example.com/app01?version=v1.0.0",
    92  			"example.com/app01:v1.0.0",
    93  		},
    94  		{
    95  			"cimd:appc:v=0:example.com/app01?version=v1.0.0",
    96  			"example.com/app01,version=v1.0.0",
    97  		},
    98  		{
    99  			"cimd:appc:v=0:example.com/app01?label01=%3F%26%2A%2F&version=v1.0.0",
   100  			"example.com/app01,version=v1.0.0,label01=?&*/",
   101  		},
   102  		{
   103  			"cimd:appc:v=0:example-app01",
   104  			"example-app01",
   105  		},
   106  		{
   107  			"cimd:appc:v=0:example-app01?version=v1.0",
   108  			"example-app01:v1.0",
   109  		},
   110  	}
   111  
   112  	for _, tt := range tests {
   113  		u, err := url.Parse(tt.uriStr)
   114  		if err != nil {
   115  			t.Fatalf("unexpected error: %v", err)
   116  		}
   117  		appc, err := NewAppc(u)
   118  		if err != nil {
   119  			t.Fatalf("unexpected error: %v", err)
   120  		}
   121  
   122  		app := appc.(*Appc).App()
   123  
   124  		expectedApp, err := discovery.NewAppFromString(tt.out)
   125  		if err != nil {
   126  			t.Fatalf("unexpected error: %v", err)
   127  		}
   128  		if !reflect.DeepEqual(app, expectedApp) {
   129  			t.Fatalf("expected app %s, but got %q", expectedApp.String(), app.String())
   130  		}
   131  	}
   132  }