github.com/ddev/ddev@v1.23.2-0.20240519125000-d824ffe36ff3/pkg/ddevapp/container_test.go (about)

     1  package ddevapp_test
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"strings"
     7  	"testing"
     8  
     9  	"github.com/ddev/ddev/pkg/ddevapp"
    10  	"github.com/ddev/ddev/pkg/util"
    11  	asrt "github.com/stretchr/testify/assert"
    12  )
    13  
    14  // Test to make sure that the user provisioned in the container has
    15  // the proper uid/gid/username characteristics.
    16  func TestUserProvisioningInContainer(t *testing.T) {
    17  	assert := asrt.New(t)
    18  	app := &ddevapp.DdevApp{}
    19  
    20  	// Make sure this leaves us in the original test directory
    21  	testDir, _ := os.Getwd()
    22  	//nolint: errcheck
    23  	defer os.Chdir(testDir)
    24  
    25  	site := TestSites[0]
    26  	switchDir := site.Chdir()
    27  	defer switchDir()
    28  
    29  	defer util.TimeTrackC(fmt.Sprintf("%s %s", site.Name, t.Name()))()
    30  
    31  	err := app.Init(site.Dir)
    32  	assert.NoError(err)
    33  
    34  	err = app.Start()
    35  	assert.NoError(err)
    36  	//nolint: errcheck
    37  	defer app.Stop(true, false)
    38  
    39  	// make sure files get created in the user?
    40  
    41  	uid, gid, username := util.GetContainerUIDGid()
    42  
    43  	for _, service := range []string{"web", "db"} {
    44  
    45  		out, _, err := app.Exec(&ddevapp.ExecOpts{
    46  			Service: service,
    47  			Cmd:     "id -un",
    48  		})
    49  		assert.NoError(err)
    50  		assert.Equal(username, strings.Trim(out, "\r\n"))
    51  
    52  		out, _, err = app.Exec(&ddevapp.ExecOpts{
    53  			Service: service,
    54  			Cmd:     "id -u",
    55  		})
    56  		assert.NoError(err)
    57  		assert.Equal(uid, strings.Trim(out, "\r\n"))
    58  
    59  		out, _, err = app.Exec(&ddevapp.ExecOpts{
    60  			Service: service,
    61  			Cmd:     "id -g",
    62  		})
    63  		assert.NoError(err)
    64  		assert.Equal(gid, strings.Trim(out, "\r\n"))
    65  	}
    66  
    67  }