github.com/terramate-io/tf@v0.0.0-20230830114523-fce866b4dfcd/cloud/cloudplan/saved_plan_test.go (about) 1 // Copyright (c) HashiCorp, Inc. 2 // SPDX-License-Identifier: MPL-2.0 3 4 package cloudplan 5 6 import ( 7 "errors" 8 "os" 9 "path/filepath" 10 "testing" 11 12 "github.com/google/go-cmp/cmp" 13 "github.com/zclconf/go-cty/cty" 14 ) 15 16 func TestCloud_loadBasic(t *testing.T) { 17 bookmark := SavedPlanBookmark{ 18 RemotePlanFormat: 1, 19 RunID: "run-GXfuHMkbyHccAGUg", 20 Hostname: "app.terraform.io", 21 } 22 23 file := "./testdata/plan-bookmark/bookmark.json" 24 result, err := LoadSavedPlanBookmark(file) 25 if err != nil { 26 t.Fatal(err) 27 } 28 29 if diff := cmp.Diff(bookmark, result, cmp.Comparer(cty.Value.RawEquals)); diff != "" { 30 t.Errorf("wrong result\n%s", diff) 31 } 32 } 33 34 func TestCloud_loadCheckRunID(t *testing.T) { 35 // Run ID must never be empty 36 file := "./testdata/plan-bookmark/empty_run_id.json" 37 _, err := LoadSavedPlanBookmark(file) 38 if !errors.Is(err, ErrInvalidRunID) { 39 t.Fatalf("expected %s but got %s", ErrInvalidRunID, err) 40 } 41 } 42 43 func TestCloud_loadCheckHostname(t *testing.T) { 44 // Hostname must never be empty 45 file := "./testdata/plan-bookmark/empty_hostname.json" 46 _, err := LoadSavedPlanBookmark(file) 47 if !errors.Is(err, ErrInvalidHostname) { 48 t.Fatalf("expected %s but got %s", ErrInvalidHostname, err) 49 } 50 } 51 52 func TestCloud_loadCheckVersionNumberBasic(t *testing.T) { 53 // remote_plan_format must be set to 1 54 // remote_plan_format and format version number are used interchangeably 55 file := "./testdata/plan-bookmark/invalid_version.json" 56 _, err := LoadSavedPlanBookmark(file) 57 if !errors.Is(err, ErrInvalidRemotePlanFormat) { 58 t.Fatalf("expected %s but got %s", ErrInvalidRemotePlanFormat, err) 59 } 60 } 61 62 func TestCloud_saveWhenFileExistsBasic(t *testing.T) { 63 tmpDir := t.TempDir() 64 tmpFile, err := os.Create(filepath.Join(tmpDir, "saved-bookmark.json")) 65 if err != nil { 66 t.Fatal("File could not be created.", err) 67 } 68 defer tmpFile.Close() 69 70 // verify the created path exists 71 // os.Stat() wants path to file 72 _, error := os.Stat(tmpFile.Name()) 73 if error != nil { 74 t.Fatal("Path to file does not exist.", error) 75 } else { 76 b := &SavedPlanBookmark{ 77 RemotePlanFormat: 1, 78 RunID: "run-GXfuHMkbyHccAGUg", 79 Hostname: "app.terraform.io", 80 } 81 err := b.Save(tmpFile.Name()) 82 if err != nil { 83 t.Fatal(err) 84 } 85 } 86 } 87 88 func TestCloud_saveWhenFileDoesNotExistBasic(t *testing.T) { 89 tmpDir := t.TempDir() 90 b := &SavedPlanBookmark{ 91 RemotePlanFormat: 1, 92 RunID: "run-GXfuHMkbyHccAGUg", 93 Hostname: "app.terraform.io", 94 } 95 err := b.Save(filepath.Join(tmpDir, "create-new-file.txt")) 96 if err != nil { 97 t.Fatal(err) 98 } 99 }