|
| 1 | +package integration |
| 2 | + |
| 3 | +import ( |
| 4 | +"context" |
| 5 | +"flag" |
| 6 | +"fmt" |
| 7 | +"os" |
| 8 | +"os/exec" |
| 9 | +"strconv" |
| 10 | +"syscall" |
| 11 | +"testing" |
| 12 | + |
| 13 | +"github.com/google/uuid" |
| 14 | +"github.com/stretchr/testify/assert" |
| 15 | +"github.com/stretchr/testify/require" |
| 16 | +"tailscale.com/tailcfg" |
| 17 | + |
| 18 | +"cdr.dev/slog" |
| 19 | +"cdr.dev/slog/sloggers/slogtest" |
| 20 | +"github.com/coder/coder/v2/tailnet" |
| 21 | +"github.com/coder/coder/v2/testutil" |
| 22 | +) |
| 23 | + |
| 24 | +var ( |
| 25 | +isChild=flag.Bool("child",false,"Run tests as a child") |
| 26 | +childTestID=flag.Int("child-test-id",0,"Which test is being run") |
| 27 | +childCoordinateURL=flag.String("child-coordinate-url","","The coordinate url to connect back to") |
| 28 | +childAgentID=flag.String("child-agent-id","","The agent id of the child") |
| 29 | +) |
| 30 | + |
| 31 | +funcTestMain(m*testing.M) { |
| 32 | +ifrun:=os.Getenv("CODER_TAILNET_TESTS");run=="" { |
| 33 | +_,_=fmt.Println("skipping tests...") |
| 34 | +return |
| 35 | +} |
| 36 | +ifos.Getuid()!=0 { |
| 37 | +_,_=fmt.Println("networking integration tests must run as root") |
| 38 | +return |
| 39 | +} |
| 40 | +flag.Parse() |
| 41 | +os.Exit(m.Run()) |
| 42 | +} |
| 43 | + |
| 44 | +vartests= []Test{{ |
| 45 | +Name:"Normal", |
| 46 | +DERPMap:DERPMapTailscale, |
| 47 | +Coordinator:CoordinatorInMemory, |
| 48 | +Parent:Parent{ |
| 49 | +NetworkSetup:NetworkSetupDefault, |
| 50 | +TailnetSetup:TailnetSetupDRPC, |
| 51 | +Run:func(ctx context.Context,t*testing.T,optsParentOpts) { |
| 52 | +reach:=opts.Conn.AwaitReachable(ctx,tailnet.IPFromUUID(opts.AgentID)) |
| 53 | +assert.True(t,reach) |
| 54 | +}, |
| 55 | +}, |
| 56 | +Child:Child{ |
| 57 | +NetworkSetup:NetworkSetupDefault, |
| 58 | +TailnetSetup:TailnetSetupDRPC, |
| 59 | +Run:func(ctx context.Context,t*testing.T,optsChildOpts) { |
| 60 | +// wait until the parent kills us |
| 61 | +<-make(chanstruct{}) |
| 62 | +}, |
| 63 | +}, |
| 64 | +}} |
| 65 | + |
| 66 | +//nolint:paralleltest |
| 67 | +funcTestIntegration(t*testing.T) { |
| 68 | +if*isChild { |
| 69 | +logger:=slogtest.Make(t,nil).Leveled(slog.LevelDebug) |
| 70 | +ctx,cancel:=context.WithTimeout(context.Background(),testutil.WaitSuperLong) |
| 71 | +t.Cleanup(cancel) |
| 72 | + |
| 73 | +agentID,err:=uuid.Parse(*childAgentID) |
| 74 | +require.NoError(t,err) |
| 75 | + |
| 76 | +test:=tests[*childTestID] |
| 77 | +test.Child.NetworkSetup(t) |
| 78 | +dm:=test.DERPMap(ctx,t) |
| 79 | +conn:=test.Child.TailnetSetup(ctx,t,logger,agentID,uuid.Nil,*childCoordinateURL,dm) |
| 80 | +test.Child.Run(ctx,t,ChildOpts{ |
| 81 | +Logger:logger, |
| 82 | +Conn:conn, |
| 83 | +AgentID:agentID, |
| 84 | +}) |
| 85 | +return |
| 86 | +} |
| 87 | + |
| 88 | +forid,test:=rangetests { |
| 89 | +t.Run(test.Name,func(t*testing.T) { |
| 90 | +ctx,cancel:=context.WithTimeout(context.Background(),testutil.WaitSuperLong) |
| 91 | +t.Cleanup(cancel) |
| 92 | + |
| 93 | +logger:=slogtest.Make(t,nil).Leveled(slog.LevelDebug) |
| 94 | +parentID,childID:=uuid.New(),uuid.New() |
| 95 | +dm:=test.DERPMap(ctx,t) |
| 96 | +_,coordURL:=test.Coordinator(t,logger,dm) |
| 97 | + |
| 98 | +child,waitChild:=execChild(ctx,id,coordURL,childID) |
| 99 | +test.Parent.NetworkSetup(t) |
| 100 | +conn:=test.Parent.TailnetSetup(ctx,t,logger,parentID,childID,coordURL,dm) |
| 101 | +test.Parent.Run(ctx,t,ParentOpts{ |
| 102 | +Logger:logger, |
| 103 | +Conn:conn, |
| 104 | +ClientID:parentID, |
| 105 | +AgentID:childID, |
| 106 | +}) |
| 107 | +child.Process.Signal(syscall.SIGINT) |
| 108 | +<-waitChild |
| 109 | +}) |
| 110 | +} |
| 111 | +} |
| 112 | + |
| 113 | +typeTeststruct { |
| 114 | +// Name is the name of the test. |
| 115 | +Namestring |
| 116 | + |
| 117 | +// DERPMap returns the DERP map to use for both the parent and child. It is |
| 118 | +// called once at the beginning of the test. |
| 119 | +DERPMapfunc(ctx context.Context,t*testing.T)*tailcfg.DERPMap |
| 120 | +// Coordinator returns a running tailnet coordinator, and the url to reach |
| 121 | +// it on. |
| 122 | +Coordinatorfunc(t*testing.T,logger slog.Logger,dm*tailcfg.DERPMap) (coord tailnet.Coordinator,urlstring) |
| 123 | + |
| 124 | +ParentParent |
| 125 | +ChildChild |
| 126 | +} |
| 127 | + |
| 128 | +// Parent is the struct containing all of the parent specific configurations. |
| 129 | +// Functions are invoked in order of struct definition. |
| 130 | +typeParentstruct { |
| 131 | +// NetworkSetup is run before all test code. It can be used to setup |
| 132 | +// networking scenarios. |
| 133 | +NetworkSetupfunc(t*testing.T) |
| 134 | + |
| 135 | +// TailnetSetup creates a tailnet network. |
| 136 | +TailnetSetupfunc( |
| 137 | +ctx context.Context,t*testing.T,logger slog.Logger, |
| 138 | +id,agentID uuid.UUID,coordURLstring,dm*tailcfg.DERPMap, |
| 139 | +)*tailnet.Conn |
| 140 | + |
| 141 | +Runfunc(ctx context.Context,t*testing.T,optsParentOpts) |
| 142 | +} |
| 143 | + |
| 144 | +// Child is the struct containing all of the child specific configurations. |
| 145 | +// Functions are invoked in order of struct definition. |
| 146 | +typeChildstruct { |
| 147 | +// NetworkSetup is run before all test code. It can be used to setup |
| 148 | +// networking scenarios. |
| 149 | +NetworkSetupfunc(t*testing.T) |
| 150 | + |
| 151 | +// TailnetSetup creates a tailnet network. |
| 152 | +TailnetSetupfunc( |
| 153 | +ctx context.Context,t*testing.T,logger slog.Logger, |
| 154 | +id,agentID uuid.UUID,coordURLstring,dm*tailcfg.DERPMap, |
| 155 | +)*tailnet.Conn |
| 156 | + |
| 157 | +// Run runs the actual test. Parents and children run in separate processes, |
| 158 | +// so it's important to ensure no communication happens over memory between |
| 159 | +// run functions of parents and children. |
| 160 | +Runfunc(ctx context.Context,t*testing.T,optsChildOpts) |
| 161 | +} |
| 162 | + |
| 163 | +typeParentOptsstruct { |
| 164 | +Logger slog.Logger |
| 165 | +Conn*tailnet.Conn |
| 166 | +ClientID uuid.UUID |
| 167 | +AgentID uuid.UUID |
| 168 | +} |
| 169 | + |
| 170 | +typeChildOptsstruct { |
| 171 | +Logger slog.Logger |
| 172 | +Conn*tailnet.Conn |
| 173 | +AgentID uuid.UUID |
| 174 | +} |
| 175 | + |
| 176 | +funcexecChild(ctx context.Context,testIDint,coordURLstring,agentID uuid.UUID) (*exec.Cmd,<-chanerror) { |
| 177 | +ch:=make(chanerror) |
| 178 | +binary:=os.Args[0] |
| 179 | +args:=os.Args[1:] |
| 180 | +args=append(args, |
| 181 | +"--child=true", |
| 182 | +"--child-test-id="+strconv.Itoa(testID), |
| 183 | +"--child-coordinate-url="+coordURL, |
| 184 | +"--child-agent-id="+agentID.String(), |
| 185 | +) |
| 186 | + |
| 187 | +cmd:=exec.CommandContext(ctx,binary,args...) |
| 188 | +cmd.Stdout=os.Stdout |
| 189 | +cmd.Stderr=os.Stderr |
| 190 | +gofunc() { |
| 191 | +ch<-cmd.Run() |
| 192 | +}() |
| 193 | +returncmd,ch |
| 194 | +} |