github.com/AliyunContainerService/cli@v0.0.0-20181009023821-814ced4b30d0/internal/licenseutils/utils_test.go (about) 1 package licenseutils 2 3 import ( 4 "context" 5 "fmt" 6 "io/ioutil" 7 "os" 8 "path/filepath" 9 "testing" 10 "time" 11 12 "github.com/docker/docker/api/types" 13 "github.com/docker/licensing/model" 14 "gotest.tools/assert" 15 ) 16 17 func TestLoginNoAuth(t *testing.T) { 18 ctx := context.Background() 19 20 _, err := Login(ctx, &types.AuthConfig{}) 21 22 assert.ErrorContains(t, err, "must be logged in") 23 } 24 25 func TestGetOrgByID(t *testing.T) { 26 orgs := []model.Org{ 27 {ID: "id1"}, 28 {ID: "id2"}, 29 } 30 u := HubUser{ 31 Orgs: orgs, 32 } 33 o, err := u.GetOrgByID("id1") 34 assert.NilError(t, err) 35 assert.Assert(t, o.ID == "id1") 36 o, err = u.GetOrgByID("id2") 37 assert.NilError(t, err) 38 assert.Assert(t, o.ID == "id2") 39 o, err = u.GetOrgByID("id3") 40 assert.ErrorContains(t, err, "not found") 41 } 42 43 func TestGetAvailableLicensesListFail(t *testing.T) { 44 ctx := context.Background() 45 user := HubUser{ 46 client: &fakeLicensingClient{ 47 listSubscriptionsFunc: func(ctx context.Context, authToken, dockerID string) (response []*model.Subscription, err error) { 48 return nil, fmt.Errorf("list subscriptions error") 49 }, 50 }, 51 } 52 _, err := user.GetAvailableLicenses(ctx) 53 assert.ErrorContains(t, err, "list subscriptions error") 54 } 55 56 func TestGetAvailableLicensesOrgFail(t *testing.T) { 57 ctx := context.Background() 58 user := HubUser{ 59 Orgs: []model.Org{ 60 {ID: "orgid"}, 61 }, 62 client: &fakeLicensingClient{ 63 listSubscriptionsFunc: func(ctx context.Context, authToken, dockerID string) (response []*model.Subscription, err error) { 64 if dockerID == "orgid" { 65 return nil, fmt.Errorf("list subscriptions org error") 66 } 67 return nil, nil 68 }, 69 }, 70 } 71 _, err := user.GetAvailableLicenses(ctx) 72 assert.ErrorContains(t, err, "list subscriptions org error") 73 } 74 75 func TestGetAvailableLicensesHappy(t *testing.T) { 76 ctx := context.Background() 77 expiration := time.Now().Add(3600 * time.Second) 78 user := HubUser{ 79 User: model.User{ 80 ID: "userid", 81 Username: "username", 82 }, 83 Orgs: []model.Org{ 84 { 85 ID: "orgid", 86 Orgname: "orgname", 87 }, 88 }, 89 client: &fakeLicensingClient{ 90 listSubscriptionsFunc: func(ctx context.Context, authToken, dockerID string) (response []*model.Subscription, err error) { 91 if dockerID == "orgid" { 92 return []*model.Subscription{ 93 { 94 State: "expired", 95 Expires: &expiration, 96 }, 97 { 98 State: "active", 99 DockerID: "orgid", 100 Expires: &expiration, 101 }, 102 { 103 State: "active", 104 DockerID: "invalidid", 105 Expires: &expiration, 106 }, 107 }, nil 108 } else if dockerID == "userid" { 109 return []*model.Subscription{ 110 { 111 State: "expired", 112 }, 113 { 114 State: "active", 115 DockerID: "userid", 116 Expires: &expiration, 117 PricingComponents: model.PricingComponents{ 118 { 119 Name: "comp1", 120 Value: 1, 121 }, 122 { 123 Name: "comp2", 124 Value: 2, 125 }, 126 }, 127 }, 128 }, nil 129 } 130 return nil, nil 131 }, 132 }, 133 } 134 subs, err := user.GetAvailableLicenses(ctx) 135 assert.NilError(t, err) 136 assert.Assert(t, len(subs) == 3) 137 assert.Assert(t, subs[0].Owner == "username") 138 assert.Assert(t, subs[0].State == "active") 139 assert.Assert(t, subs[0].ComponentsString == "comp1:1,comp2:2") 140 assert.Assert(t, subs[1].Owner == "orgname") 141 assert.Assert(t, subs[1].State == "active") 142 assert.Assert(t, subs[2].Owner == "unknown") 143 assert.Assert(t, subs[2].State == "active") 144 } 145 146 func TestGenerateTrialFail(t *testing.T) { 147 ctx := context.Background() 148 user := HubUser{ 149 client: &fakeLicensingClient{ 150 generateNewTrialSubscriptionFunc: func(ctx context.Context, authToken, dockerID, email string) (subscriptionID string, err error) { 151 return "", fmt.Errorf("generate trial failure") 152 }, 153 }, 154 } 155 targetID := "targetidgoeshere" 156 _, err := user.GenerateTrialLicense(ctx, targetID) 157 assert.ErrorContains(t, err, "generate trial failure") 158 } 159 160 func TestGenerateTrialHappy(t *testing.T) { 161 ctx := context.Background() 162 user := HubUser{ 163 client: &fakeLicensingClient{ 164 generateNewTrialSubscriptionFunc: func(ctx context.Context, authToken, dockerID, email string) (subscriptionID string, err error) { 165 return "subid", nil 166 }, 167 }, 168 } 169 targetID := "targetidgoeshere" 170 _, err := user.GenerateTrialLicense(ctx, targetID) 171 assert.NilError(t, err) 172 } 173 174 func TestGetIssuedLicense(t *testing.T) { 175 ctx := context.Background() 176 user := HubUser{ 177 client: &fakeLicensingClient{}, 178 } 179 id := "idgoeshere" 180 _, err := user.GetIssuedLicense(ctx, id) 181 assert.NilError(t, err) 182 } 183 184 func TestLoadLocalIssuedLicenseNotExist(t *testing.T) { 185 ctx := context.Background() 186 tmpdir, err := ioutil.TempDir("", "licensing-test") 187 assert.NilError(t, err) 188 defer os.RemoveAll(tmpdir) 189 filename := filepath.Join(tmpdir, "subscription.lic") 190 _, err = LoadLocalIssuedLicense(ctx, filename) 191 assert.ErrorContains(t, err, "no such file") 192 } 193 194 func TestLoadLocalIssuedLicenseNotJson(t *testing.T) { 195 ctx := context.Background() 196 tmpdir, err := ioutil.TempDir("", "licensing-test") 197 assert.NilError(t, err) 198 defer os.RemoveAll(tmpdir) 199 filename := filepath.Join(tmpdir, "subscription.lic") 200 err = ioutil.WriteFile(filename, []byte("not json"), 0644) 201 assert.NilError(t, err) 202 _, err = LoadLocalIssuedLicense(ctx, filename) 203 assert.ErrorContains(t, err, "malformed license file") 204 } 205 206 func TestLoadLocalIssuedLicenseNoVerify(t *testing.T) { 207 lclient := &fakeLicensingClient{ 208 verifyLicenseFunc: func(ctx context.Context, license model.IssuedLicense) (res *model.CheckResponse, err error) { 209 return nil, fmt.Errorf("verification failed") 210 }, 211 } 212 ctx := context.Background() 213 tmpdir, err := ioutil.TempDir("", "licensing-test") 214 assert.NilError(t, err) 215 defer os.RemoveAll(tmpdir) 216 filename := filepath.Join(tmpdir, "subscription.lic") 217 err = ioutil.WriteFile(filename, []byte("{}"), 0644) 218 assert.NilError(t, err) 219 _, err = doLoadLocalIssuedLicense(ctx, filename, lclient) 220 assert.ErrorContains(t, err, "verification failed") 221 } 222 223 func TestLoadLocalIssuedLicenseHappy(t *testing.T) { 224 lclient := &fakeLicensingClient{} 225 ctx := context.Background() 226 tmpdir, err := ioutil.TempDir("", "licensing-test") 227 assert.NilError(t, err) 228 defer os.RemoveAll(tmpdir) 229 filename := filepath.Join(tmpdir, "subscription.lic") 230 err = ioutil.WriteFile(filename, []byte("{}"), 0644) 231 assert.NilError(t, err) 232 _, err = doLoadLocalIssuedLicense(ctx, filename, lclient) 233 assert.NilError(t, err) 234 }