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