github.com/hernad/nomad@v1.6.112/e2e/workload_id/taskapi_test.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: MPL-2.0
     3  
     4  package workload_id
     5  
     6  import (
     7  	"fmt"
     8  	"io"
     9  	"net/http"
    10  	"strings"
    11  	"testing"
    12  
    13  	"github.com/hernad/nomad/e2e/e2eutil"
    14  	"github.com/hernad/nomad/helper/uuid"
    15  	"github.com/shoenig/test"
    16  	"github.com/shoenig/test/must"
    17  )
    18  
    19  // TestTaskAPI runs subtests exercising the Task API related functionality.
    20  // Bundled with Workload Identity as that's a prereq for the Task API to work.
    21  func TestTaskAPI(t *testing.T) {
    22  	nomad := e2eutil.NomadClient(t)
    23  
    24  	e2eutil.WaitForLeader(t, nomad)
    25  	e2eutil.WaitForNodesReady(t, nomad, 1)
    26  
    27  	t.Run("testTaskAPI_Auth", testTaskAPIAuth)
    28  	t.Run("testTaskAPI_Windows", testTaskAPIWindows)
    29  }
    30  
    31  func testTaskAPIAuth(t *testing.T) {
    32  	nomad := e2eutil.NomadClient(t)
    33  	jobID := "api-auth-" + uuid.Short()
    34  	jobIDs := []string{jobID}
    35  	t.Cleanup(e2eutil.CleanupJobsAndGC(t, &jobIDs))
    36  
    37  	// start job
    38  	allocs := e2eutil.RegisterAndWaitForAllocs(t, nomad, "./input/api-auth.nomad.hcl", jobID, "")
    39  	must.Len(t, 1, allocs)
    40  	allocID := allocs[0].ID
    41  
    42  	// wait for batch alloc to complete
    43  	alloc := e2eutil.WaitForAllocStopped(t, nomad, allocID)
    44  	must.Eq(t, alloc.ClientStatus, "complete")
    45  
    46  	assertions := []struct {
    47  		task   string
    48  		suffix string
    49  	}{
    50  		{
    51  			task:   "none",
    52  			suffix: http.StatusText(http.StatusUnauthorized),
    53  		},
    54  		{
    55  			task:   "bad",
    56  			suffix: http.StatusText(http.StatusForbidden),
    57  		},
    58  		{
    59  			task:   "docker-wid",
    60  			suffix: `"ok":true}}`,
    61  		},
    62  		{
    63  			task:   "exec-wid",
    64  			suffix: `"ok":true}}`,
    65  		},
    66  	}
    67  
    68  	// Ensure the assertions and input file match
    69  	must.Len(t, len(assertions), alloc.Job.TaskGroups[0].Tasks,
    70  		must.Sprintf("test and jobspec mismatch"))
    71  
    72  	for _, tc := range assertions {
    73  		logFile := fmt.Sprintf("alloc/logs/%s.stdout.0", tc.task)
    74  		fd, err := nomad.AllocFS().Cat(alloc, logFile, nil)
    75  		must.NoError(t, err)
    76  		logBytes, err := io.ReadAll(fd)
    77  		must.NoError(t, err)
    78  		logs := string(logBytes)
    79  
    80  		ps := must.Sprintf("Task: %s Logs: <<EOF\n%sEOF", tc.task, logs)
    81  
    82  		must.StrHasSuffix(t, tc.suffix, logs, ps)
    83  	}
    84  }
    85  
    86  func testTaskAPIWindows(t *testing.T) {
    87  	nomad := e2eutil.NomadClient(t)
    88  	winNodes, err := e2eutil.ListWindowsClientNodes(nomad)
    89  	must.NoError(t, err)
    90  	if len(winNodes) == 0 {
    91  		t.Skip("no Windows clients")
    92  	}
    93  
    94  	found := false
    95  	for _, nodeID := range winNodes {
    96  		node, _, err := nomad.Nodes().Info(nodeID, nil)
    97  		must.NoError(t, err)
    98  		if name := node.Attributes["os.name"]; strings.Contains(name, "2016") {
    99  			t.Logf("Node %s is too old to support unix sockets: %s", nodeID, name)
   100  			continue
   101  		}
   102  
   103  		found = true
   104  		break
   105  	}
   106  	if !found {
   107  		t.Skip("no Windows clients with unix socket support")
   108  	}
   109  
   110  	jobID := "api-win-" + uuid.Short()
   111  	jobIDs := []string{jobID}
   112  	t.Cleanup(e2eutil.CleanupJobsAndGC(t, &jobIDs))
   113  
   114  	// start job
   115  	allocs := e2eutil.RegisterAndWaitForAllocs(t, nomad, "./input/api-win.nomad.hcl", jobID, "")
   116  	must.Len(t, 1, allocs)
   117  	allocID := allocs[0].ID
   118  
   119  	// wait for batch alloc to complete
   120  	alloc := e2eutil.WaitForAllocStopped(t, nomad, allocID)
   121  	test.Eq(t, alloc.ClientStatus, "complete")
   122  
   123  	logFile := "alloc/logs/win.stdout.0"
   124  	fd, err := nomad.AllocFS().Cat(alloc, logFile, nil)
   125  	must.NoError(t, err)
   126  	logBytes, err := io.ReadAll(fd)
   127  	must.NoError(t, err)
   128  	logs := string(logBytes)
   129  
   130  	must.StrHasSuffix(t, `"ok":true}}`, logs)
   131  }