refactor: add err checks (#2896)

Signed-off-by: Larry Bordowitz <laurence.bordowitz@gmail.com>
This commit is contained in:
Larry Bordowitz
2025-06-17 11:18:53 -05:00
committed by GitHub
parent 952c7b255f
commit ed6699f8c9
32 changed files with 271 additions and 121 deletions

View File

@@ -324,7 +324,9 @@ in order to capture the filesystem context the remote workspace expects:
log.Printf("[ERROR] error searching process ID: %v", err)
return
}
p.Signal(syscall.SIGINT)
if err := p.Signal(syscall.SIGINT); err != nil {
log.Printf("[ERROR] error sending interrupt signal: %v", err)
}
}
}
}()

View File

@@ -84,7 +84,7 @@ func TestCloud_runTaskStageWithPolicyEvaluation(t *testing.T) {
trs := policyEvaluationSummarizer{
cloud: b,
}
c.context.Poll(0, 0, func(i int) (bool, error) {
err := c.context.Poll(0, 0, func(i int) (bool, error) {
cont, _, _ := trs.Summarize(c.context, c.writer, c.taskStage())
if cont {
return true, nil
@@ -98,5 +98,8 @@ func TestCloud_runTaskStageWithPolicyEvaluation(t *testing.T) {
}
return false, nil
})
if err != nil {
t.Fatalf("Error while polling: %v", err)
}
}
}

View File

@@ -155,7 +155,7 @@ func TestCloud_runTasksWithTaskResults(t *testing.T) {
trs := taskResultSummarizer{
cloud: b,
}
c.context.Poll(0, 0, func(i int) (bool, error) {
err := c.context.Poll(0, 0, func(i int) (bool, error) {
cont, _, _ := trs.Summarize(c.context, c.writer, c.taskStage())
if cont {
return true, nil
@@ -169,5 +169,8 @@ func TestCloud_runTasksWithTaskResults(t *testing.T) {
}
return false, nil
})
if err != nil {
t.Fatalf("Error while polling: %v", err)
}
}
}

View File

@@ -136,7 +136,10 @@ func testRunner(t *testing.T, cases testCases, orgCount int, tfEnvFlags ...strin
if lenInput > 0 {
for i := 0; i < lenInput; i++ {
input := tfCmd.userInput[i]
exp.SendLine(input)
_, err := exp.SendLine(input)
if err != nil {
subtest.Fatal(err)
}
// use the index to find the corresponding
// output that matches the input.
if lenInputOutput-1 >= i {
@@ -183,6 +186,13 @@ func setTfeClient() {
}
}
func chdirOCF(dir string) {
if err := os.Chdir(dir); err != nil {
fmt.Printf("Could not change directories: %v\n", err)
os.Exit(1)
}
}
func setupBinary() func() {
log.Println("Setting up terraform binary")
tmpTerraformBinaryDir, err := os.MkdirTemp("", "terraform-test")
@@ -192,9 +202,9 @@ func setupBinary() func() {
}
log.Println(tmpTerraformBinaryDir)
currentDir, err := os.Getwd()
defer os.Chdir(currentDir)
defer chdirOCF(currentDir)
if err != nil {
fmt.Printf("Could not change directories: %v\n", err)
fmt.Printf("Could not get current directory: %v\n", err)
os.Exit(1)
}
// Getting top level dir
@@ -204,10 +214,7 @@ func setupBinary() func() {
topLevel := len(dirPaths) - 3
topDir := strings.Join(dirPaths[0:topLevel], "/")
if err := os.Chdir(topDir); err != nil {
fmt.Printf("Could not change directories: %v\n", err)
os.Exit(1)
}
chdirOCF(topDir)
cmd := exec.Command(
"go",

View File

@@ -182,7 +182,7 @@ func TestCloudLocks(t *testing.T) {
_, err = lockerB.Lock(infoB)
if err == nil {
lockerA.Unlock(lockIDA)
_ = lockerA.Unlock(lockIDA) // test already failed, no need to check err further
t.Fatal("client B obtained lock while held by client A")
}
if _, ok := err.(*statemgr.LockError); !ok {
@@ -335,12 +335,15 @@ func TestState_PersistState(t *testing.T) {
if err != nil {
t.Fatal(err)
}
cloudState.WriteState(states.BuildState(func(s *states.SyncState) {
err = cloudState.WriteState(states.BuildState(func(s *states.SyncState) {
s.SetOutputValue(
addrs.OutputValue{Name: "boop"}.Absolute(addrs.RootModuleInstance),
cty.StringVal("beep"), false, "",
)
}))
if err != nil {
t.Fatal(err)
}
err = cloudState.PersistState(nil)
if err != nil {

View File

@@ -40,10 +40,6 @@ import (
"github.com/opentofu/opentofu/internal/tofu"
)
const (
testCred = "test-auth-token"
)
var (
tfeHost = "app.terraform.io"
credsSrc = svcauth.StaticCredentialsSource(map[svchost.Hostname]svcauth.HostCredentials{
@@ -424,12 +420,18 @@ func testServerWithSnapshotsEnabled(t *testing.T, enabled bool) *httptest.Server
fakeState := states.NewState()
fakeStateFile := statefile.New(fakeState, "boop", 1)
var buf bytes.Buffer
statefile.Write(fakeStateFile, &buf, encryption.StateEncryptionDisabled())
err := statefile.Write(fakeStateFile, &buf, encryption.StateEncryptionDisabled())
if err != nil {
t.Fatal(err)
}
respBody := buf.Bytes()
w.Header().Set("content-type", "application/json")
w.Header().Set("content-length", strconv.FormatInt(int64(len(respBody)), 10))
w.WriteHeader(http.StatusOK)
w.Write(respBody)
_, err = w.Write(respBody)
if err != nil {
t.Fatal(err)
}
return
}
@@ -477,7 +479,10 @@ func testServerWithSnapshotsEnabled(t *testing.T, enabled bool) *httptest.Server
}
w.WriteHeader(http.StatusOK)
w.Write(fakeBodyRaw)
_, err = w.Write(fakeBodyRaw)
if err != nil {
t.Fatal(err)
}
}))
serverURL = server.URL
return server
@@ -490,20 +495,26 @@ var testDefaultRequestHandlers = map[string]func(http.ResponseWriter, *http.Requ
// Respond to service discovery calls.
"/well-known/terraform.json": func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
io.WriteString(w, `{
_, err := io.WriteString(w, `{
"tfe.v2": "/api/v2/",
}`)
if err != nil {
w.WriteHeader(500)
}
},
// Respond to service version constraints calls.
"/v1/versions/": func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
io.WriteString(w, fmt.Sprintf(`{
_, err := io.WriteString(w, fmt.Sprintf(`{
"service": "%s",
"product": "terraform",
"minimum": "0.1.0",
"maximum": "10.0.0"
}`, path.Base(r.URL.Path)))
if err != nil {
w.WriteHeader(500)
}
},
// Respond to pings to get the API version header.
@@ -515,7 +526,7 @@ var testDefaultRequestHandlers = map[string]func(http.ResponseWriter, *http.Requ
// Respond to the initial query to read the hashicorp org entitlements.
"/api/v2/organizations/hashicorp/entitlement-set": func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/vnd.api+json")
io.WriteString(w, `{
_, err := io.WriteString(w, `{
"data": {
"id": "org-GExadygjSbKP8hsY",
"type": "entitlement-sets",
@@ -529,12 +540,15 @@ var testDefaultRequestHandlers = map[string]func(http.ResponseWriter, *http.Requ
}
}
}`)
if err != nil {
w.WriteHeader(500)
}
},
// Respond to the initial query to read the no-operations org entitlements.
"/api/v2/organizations/no-operations/entitlement-set": func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/vnd.api+json")
io.WriteString(w, `{
_, err := io.WriteString(w, `{
"data": {
"id": "org-ufxa3y8jSbKP8hsT",
"type": "entitlement-sets",
@@ -548,13 +562,16 @@ var testDefaultRequestHandlers = map[string]func(http.ResponseWriter, *http.Requ
}
}
}`)
if err != nil {
w.WriteHeader(500)
}
},
// All tests that are assumed to pass will use the hashicorp organization,
// so for all other organization requests we will return a 404.
"/api/v2/organizations/": func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(404)
io.WriteString(w, `{
_, err := io.WriteString(w, `{
"errors": [
{
"status": "404",
@@ -562,6 +579,9 @@ var testDefaultRequestHandlers = map[string]func(http.ResponseWriter, *http.Requ
}
]
}`)
if err != nil {
w.WriteHeader(500)
}
},
}