Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -1072,7 +1072,7 @@ integration: $(TEST_LOG_DIR) ensure-gotestsum
INTEGRATION_KUBE_REGEX := TestKube.*
.PHONY: integration-kube
integration-kube: FLAGS ?= -v -race
integration-kube: PACKAGES = $(shell go list ./... | grep 'integration\([^s]\|$$\)')
integration-kube: PACKAGES = $(shell go list ./... | grep 'integration\([^s]\|$$\)' | grep -v 'integration/autoupdate')
integration-kube: $(TEST_LOG_DIR) ensure-gotestsum
@echo KUBECONFIG is: $(KUBECONFIG), TEST_KUBE: $(TEST_KUBE)
$(CGOFLAG) go test -json -run "$(INTEGRATION_KUBE_REGEX)" $(PACKAGES) $(FLAGS) \
Expand Down
79 changes: 47 additions & 32 deletions integration/autoupdate/tools/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
"net/http"
"net/http/httptest"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
Expand All @@ -38,9 +37,13 @@ import (
"github.com/gravitational/trace"

"github.com/gravitational/teleport/api/constants"
"github.com/gravitational/teleport/integration/autoupdate/tools/updater/tctl"
"github.com/gravitational/teleport/integration/autoupdate/tools/updater/tsh"
"github.com/gravitational/teleport/integration/helpers/archive"
"github.com/gravitational/teleport/lib/autoupdate"
"github.com/gravitational/teleport/lib/modules"
"github.com/gravitational/teleport/lib/modules/modulestest"
"github.com/gravitational/teleport/lib/utils"
)

const (
Expand All @@ -51,8 +54,10 @@ const (
var (
// testVersions list of the pre-compiled binaries with encoded versions to check.
testVersions = []string{
"1.2.3",
"3.2.1",
"1.0.0",
"2.0.0",
"3.0.0",
"4.0.0",
}
limitedWriter = newLimitedResponseWriter()

Expand All @@ -61,6 +66,19 @@ var (
)

func TestMain(m *testing.M) {
executable, err := os.Executable()
if err != nil {
log.Fatal("failed to get executable name", err)
}
switch filepath.Base(executable) {
case "tsh", "tsh.exe":
tsh.Main()
return
case "tctl", "tctl.exe":
tctl.Main()
return
}

modules.SetInsecureTestMode(true)
modules.SetModules(&modulestest.Modules{TestBuildType: modules.BuildCommunity})
ctx := context.Background()
Expand All @@ -74,6 +92,12 @@ func TestMain(m *testing.M) {
log.Fatalf("failed to create temporary directory: %v", err)
}

for _, version := range testVersions {
if err := buildAndArchiveApps(ctx, tmp, version); err != nil {
log.Fatalf("failed to build testing app binary archive: %v", err)
}
}

server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
filePath := filepath.Join(tmp, r.URL.Path)
switch {
Expand All @@ -84,10 +108,8 @@ func TestMain(m *testing.M) {
}
}))
baseURL = server.URL
for _, version := range testVersions {
if err := buildAndArchiveApps(ctx, tmp, version, server.URL); err != nil {
log.Fatalf("failed to build testing app binary archive: %v", err)
}
if err := os.Setenv(autoupdate.BaseURLEnvVar, server.URL); err != nil {
log.Fatalf("failed to set base URL environment variable: %v", err)
}

// Run tests after binary is built.
Expand All @@ -100,6 +122,9 @@ func TestMain(m *testing.M) {
if err := os.RemoveAll(toolsDir); err != nil {
log.Fatalf("failed to remove tools directory: %v", err)
}
if err := os.Unsetenv(autoupdate.BaseURLEnvVar); err != nil {
log.Fatalf("failed to unset %q environment variable: %v", autoupdate.BaseURLEnvVar, err)
}

os.Exit(code)
}
Expand Down Expand Up @@ -133,18 +158,25 @@ func serve256File(w http.ResponseWriter, _ *http.Request, filePath string) {
}

// buildAndArchiveApps compiles the updater integration and pack it depends on platform is used.
func buildAndArchiveApps(ctx context.Context, path string, version string, baseURL string) error {
func buildAndArchiveApps(ctx context.Context, path string, version string) error {
versionPath := filepath.Join(path, version)
for _, app := range []string{"tsh", "tctl"} {
output := filepath.Join(versionPath, app)
output := filepath.Join(versionPath, version, app)
switch runtime.GOOS {
case constants.WindowsOS:
output = filepath.Join(versionPath, app+".exe")
output = filepath.Join(versionPath, version, app+".exe")
case constants.DarwinOS:
output = filepath.Join(versionPath, app+".app", "Contents", "MacOS", app)
output = filepath.Join(versionPath, app+".app", "Contents", "MacOS", version, app)
}
if err := buildBinary(output, version, baseURL, app); err != nil {
return trace.Wrap(err)
if err := os.MkdirAll(filepath.Dir(output), 0755); err != nil {
return err
}
testBinary, err := os.Executable()
if err != nil {
return err
}
if err := utils.CopyFile(testBinary, output, 0o755); err != nil {
return err
}
}
switch runtime.GOOS {
Expand All @@ -153,26 +185,9 @@ func buildAndArchiveApps(ctx context.Context, path string, version string, baseU
return trace.Wrap(archive.CompressDirToPkgFile(ctx, versionPath, archivePath, "com.example.pkgtest"))
case constants.WindowsOS:
archivePath := filepath.Join(path, fmt.Sprintf("teleport-v%s-windows-amd64-bin.zip", version))
return trace.Wrap(archive.CompressDirToZipFile(ctx, versionPath, archivePath))
return trace.Wrap(archive.CompressDirToZipFile(ctx, versionPath, archivePath, archive.WithNoCompress()))
default:
archivePath := filepath.Join(path, fmt.Sprintf("teleport-v%s-linux-%s-bin.tar.gz", version, runtime.GOARCH))
return trace.Wrap(archive.CompressDirToTarGzFile(ctx, versionPath, archivePath))
return trace.Wrap(archive.CompressDirToTarGzFile(ctx, versionPath, archivePath, archive.WithNoCompress()))
}
}

// buildBinary executes command to build client tool binary with updater logic for testing.
func buildBinary(output string, version string, baseURL string, app string) error {
cmd := exec.Command(
"go", "build", "-o", output,
"-ldflags", strings.Join([]string{
fmt.Sprintf("-X 'github.com/gravitational/teleport/integration/autoupdate/tools/updater.version=%s'", version),
fmt.Sprintf("-X 'github.com/gravitational/teleport/lib/autoupdate/tools.version=%s'", version),
fmt.Sprintf("-X 'github.com/gravitational/teleport/lib/autoupdate/tools.baseURL=%s'", baseURL),
}, " "),
fmt.Sprintf("./updater/%s", app),
)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

return trace.Wrap(cmd.Run())
}
22 changes: 17 additions & 5 deletions integration/autoupdate/tools/updater/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,18 @@ import (
"crypto"
"fmt"
"os"
"path/filepath"
"strings"
"time"

"github.com/gravitational/trace"

"github.com/gravitational/teleport"
"github.com/gravitational/teleport/api/types"
"github.com/gravitational/teleport/api/types/accesslist"
"github.com/gravitational/teleport/api/utils/keys"
"github.com/gravitational/teleport/api/utils/keys/hardwarekey"
"github.com/gravitational/teleport/entitlements"
"github.com/gravitational/teleport/lib/autoupdate/tools"
"github.com/gravitational/teleport/lib/modules"
"github.com/gravitational/teleport/lib/tlsca"
)
Expand All @@ -44,9 +46,19 @@ const (
TestBuild = "UPDATER_TEST_BUILD"
)

var (
version = teleport.Version
)
func init() {
path, err := os.Executable()
if err != nil {
return
}
// For the integration test we use a pattern where the version is encoded
// in the directory name to simplify usage and avoid recompiling each
// individual binary.
parts := strings.Split(path, string(filepath.Separator))
if len(parts) > 2 {
tools.Version = parts[len(parts)-2]
}
}

type TestModules struct{}

Expand Down Expand Up @@ -83,7 +95,7 @@ func (p *TestModules) LicenseExpiry() time.Time {

// PrintVersion prints the Teleport version.
func (p *TestModules) PrintVersion() {
fmt.Printf("Teleport v%v git\n", version)
fmt.Printf("Teleport v%v git\n", tools.Version)
}

// Features returns supported features
Expand Down
4 changes: 2 additions & 2 deletions integration/autoupdate/tools/updater/tctl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package main
package tctl

import (
"context"
Expand All @@ -27,7 +27,7 @@ import (
tctl "github.com/gravitational/teleport/tool/tctl/common"
)

func main() {
func Main() {
ctx, cancel := stacksignal.GetSignalHandler().NotifyContext(context.Background())
defer cancel()

Expand Down
4 changes: 2 additions & 2 deletions integration/autoupdate/tools/updater/tsh/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package main
package tsh

import (
"context"
Expand All @@ -30,7 +30,7 @@ import (
tsh "github.com/gravitational/teleport/tool/tsh/common"
)

func main() {
func Main() {
ctx, cancel := stacksignal.GetSignalHandler().NotifyContext(context.Background())
defer cancel()

Expand Down
1 change: 1 addition & 0 deletions integration/autoupdate/tools/updater_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ func TestUpdateForOSSBuild(t *testing.T) {

// Enable OSS build.
t.Setenv(updater.TestBuild, modules.BuildOSS)
t.Setenv(autoupdate.BaseURLEnvVar, "")

// Fetch compiled test binary with updater logic and install to $TELEPORT_HOME.
updater := tools.NewUpdater(
Expand Down
Loading
Loading