github.com/juju/juju@v0.0.0-20240327075706-a90865de2538/core/charm/origin_test.go (about) 1 // Copyright 2020 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package charm_test 5 6 import ( 7 "github.com/juju/testing" 8 jc "github.com/juju/testing/checkers" 9 gc "gopkg.in/check.v1" 10 11 "github.com/juju/juju/core/charm" 12 ) 13 14 type sourceSuite struct { 15 testing.IsolationSuite 16 } 17 18 var _ = gc.Suite(&sourceSuite{}) 19 20 func (s sourceSuite) TestMatches(c *gc.C) { 21 ok := charm.Source("xxx").Matches("xxx") 22 c.Assert(ok, jc.IsTrue) 23 } 24 25 func (s sourceSuite) TestNotMatches(c *gc.C) { 26 ok := charm.Source("xxx").Matches("yyy") 27 c.Assert(ok, jc.IsFalse) 28 } 29 30 type platformSuite struct { 31 testing.IsolationSuite 32 } 33 34 var _ = gc.Suite(&platformSuite{}) 35 36 func (s platformSuite) TestParsePlatform(c *gc.C) { 37 tests := []struct { 38 Name string 39 Value string 40 Expected charm.Platform 41 ExpectedErr string 42 }{{ 43 Name: "empty", 44 Value: "", 45 ExpectedErr: "platform cannot be empty", 46 }, { 47 Name: "empty components", 48 Value: "//", 49 ExpectedErr: `architecture in platform "//" not valid`, 50 }, { 51 Name: "too many components", 52 Value: "////", 53 ExpectedErr: `platform is malformed and has too many components "////"`, 54 }, { 55 Name: "architecture and channel, no os name", 56 Value: "amd64/18.04", 57 ExpectedErr: `channel without os name in platform "amd64/18.04" not valid`, 58 }, { 59 Name: "architecture", 60 Value: "amd64", 61 Expected: charm.Platform{ 62 Architecture: "amd64", 63 }, 64 }, { 65 Name: "architecture, os and series", 66 Value: "amd64/os/series", 67 Expected: charm.Platform{ 68 Architecture: "amd64", 69 OS: "os", 70 Channel: "series", 71 }, 72 }, { 73 Name: "architecture, os, version and risk", 74 Value: "amd64/os/version/risk", 75 Expected: charm.Platform{ 76 Architecture: "amd64", 77 OS: "os", 78 Channel: "version/risk", 79 }, 80 }, { 81 Name: "architecture, unknown os and series", 82 Value: "amd64/unknown/series", 83 Expected: charm.Platform{ 84 Architecture: "amd64", 85 OS: "", 86 Channel: "series", 87 }, 88 }, { 89 Name: "architecture, unknown os and unknown series", 90 Value: "amd64/unknown/unknown", 91 Expected: charm.Platform{ 92 Architecture: "amd64", 93 OS: "", 94 Channel: "", 95 }, 96 }, { 97 Name: "architecture and unknown series", 98 Value: "amd64/unknown", 99 Expected: charm.Platform{ 100 Architecture: "amd64", 101 OS: "", 102 Channel: "", 103 }, 104 }} 105 for k, test := range tests { 106 c.Logf("test %q at %d", test.Name, k) 107 ch, err := charm.ParsePlatformNormalize(test.Value) 108 if test.ExpectedErr != "" { 109 c.Assert(err, gc.ErrorMatches, test.ExpectedErr) 110 } else { 111 c.Assert(ch, gc.DeepEquals, test.Expected) 112 c.Assert(err, gc.IsNil) 113 } 114 } 115 } 116 117 func (s platformSuite) TestString(c *gc.C) { 118 tests := []struct { 119 Name string 120 Value string 121 Expected string 122 }{{ 123 Name: "architecture", 124 Value: "amd64", 125 Expected: "amd64", 126 }, { 127 Name: "architecture, os and series", 128 Value: "amd64/os/series", 129 Expected: "amd64/os/series", 130 }, { 131 Name: "architecture, os, version and risk", 132 Value: "amd64/os/version/risk", 133 Expected: "amd64/os/version/risk", 134 }} 135 for k, test := range tests { 136 c.Logf("test %q at %d", test.Name, k) 137 platform, err := charm.ParsePlatformNormalize(test.Value) 138 c.Assert(err, gc.IsNil) 139 c.Assert(platform.String(), gc.DeepEquals, test.Expected) 140 } 141 }