github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/ccl/utilccl/license_check_test.go (about)

     1  // Copyright 2017 The Cockroach Authors.
     2  //
     3  // Licensed as a CockroachDB Enterprise file under the Cockroach Community
     4  // License (the "License"); you may not use this file except in compliance with
     5  // the License. You may obtain a copy of the License at
     6  //
     7  //     https://github.com/cockroachdb/cockroach/blob/master/licenses/CCL.txt
     8  
     9  package utilccl
    10  
    11  import (
    12  	"testing"
    13  	"time"
    14  
    15  	"github.com/cockroachdb/cockroach/pkg/ccl/utilccl/licenseccl"
    16  	"github.com/cockroachdb/cockroach/pkg/settings/cluster"
    17  	"github.com/cockroachdb/cockroach/pkg/testutils"
    18  	"github.com/cockroachdb/cockroach/pkg/util/timeutil"
    19  	"github.com/cockroachdb/cockroach/pkg/util/uuid"
    20  )
    21  
    22  func TestSettingAndCheckingLicense(t *testing.T) {
    23  	idA, _ := uuid.FromString("A0000000-0000-0000-0000-00000000000A")
    24  	idB, _ := uuid.FromString("B0000000-0000-0000-0000-00000000000B")
    25  
    26  	t0 := timeutil.Unix(0, 0)
    27  
    28  	licA, _ := licenseccl.License{
    29  		ClusterID:         []uuid.UUID{idA},
    30  		Type:              licenseccl.License_Enterprise,
    31  		ValidUntilUnixSec: t0.AddDate(0, 1, 0).Unix(),
    32  	}.Encode()
    33  
    34  	licB, _ := licenseccl.License{
    35  		ClusterID:         []uuid.UUID{idB},
    36  		Type:              licenseccl.License_Evaluation,
    37  		ValidUntilUnixSec: t0.AddDate(0, 2, 0).Unix(),
    38  	}.Encode()
    39  
    40  	st := cluster.MakeTestingClusterSettings()
    41  
    42  	for i, tc := range []struct {
    43  		lic          string
    44  		checkCluster uuid.UUID
    45  		checkTime    time.Time
    46  		err          string
    47  	}{
    48  		// NB: we're observing the update manifest as changed behavior -- detailed
    49  		// testing of that behavior is left to licenseccl's own tests.
    50  		{"", idA, t0, "requires an enterprise license"},
    51  		// adding a valid lic.
    52  		{licA, idA, t0, ""},
    53  		// clearing an existing lic.
    54  		{"", idA, t0, "requires an enterprise license"},
    55  		// adding invalid lic.
    56  		{licB, idA, t0, "not valid for cluster"},
    57  		// clearing an existing, invalid lic.
    58  		{"", idA, t0, "requires an enterprise license"},
    59  	} {
    60  		updater := st.MakeUpdater()
    61  		if err := updater.Set("enterprise.license", tc.lic, "s"); err != nil {
    62  			t.Fatal(err)
    63  		}
    64  		err := checkEnterpriseEnabledAt(st, tc.checkTime, tc.checkCluster, "", "")
    65  		if !testutils.IsError(err, tc.err) {
    66  			l, _ := licenseccl.Decode(tc.lic)
    67  			t.Fatalf("%d: lic %v, update by %T, checked by %s at %s, got %q", i, l, updater, tc.checkCluster, tc.checkTime, err)
    68  		}
    69  	}
    70  }
    71  
    72  func TestGetLicenseTypePresent(t *testing.T) {
    73  	for _, tc := range []struct {
    74  		licenseType licenseccl.License_Type
    75  		expected    string
    76  	}{
    77  		{licenseccl.License_NonCommercial, "NonCommercial"},
    78  		{licenseccl.License_Enterprise, "Enterprise"},
    79  		{licenseccl.License_Evaluation, "Evaluation"},
    80  	} {
    81  		st := cluster.MakeTestingClusterSettings()
    82  		updater := st.MakeUpdater()
    83  		lic, _ := licenseccl.License{
    84  			ClusterID:         []uuid.UUID{},
    85  			Type:              tc.licenseType,
    86  			ValidUntilUnixSec: 0,
    87  		}.Encode()
    88  		if err := updater.Set("enterprise.license", lic, "s"); err != nil {
    89  			t.Fatal(err)
    90  		}
    91  		actual, err := getLicenseType(st)
    92  		if err != nil {
    93  			t.Fatal(err)
    94  		}
    95  		if actual != tc.expected {
    96  			t.Fatalf("expected license type %s, got %s", tc.expected, actual)
    97  		}
    98  	}
    99  }
   100  
   101  func TestGetLicenseTypeAbsent(t *testing.T) {
   102  	expected := "None"
   103  	actual, err := getLicenseType(cluster.MakeTestingClusterSettings())
   104  	if err != nil {
   105  		t.Fatal(err)
   106  	}
   107  	if actual != expected {
   108  		t.Fatalf("expected license type %s, got %s", expected, actual)
   109  	}
   110  }
   111  
   112  func TestSettingBadLicenseStrings(t *testing.T) {
   113  	for _, tc := range []struct{ lic, err string }{
   114  		{"blah", "invalid license string"},
   115  		{"cl-0-blah", "invalid license string"},
   116  	} {
   117  		st := cluster.MakeTestingClusterSettings()
   118  		u := st.MakeUpdater()
   119  
   120  		if err := u.Set("enterprise.license", tc.lic, "s"); !testutils.IsError(
   121  			err, tc.err,
   122  		) {
   123  			t.Fatalf("%q: expected err %q, got %v", tc.lic, tc.err, err)
   124  		}
   125  	}
   126  }