Skip to content

Commit

Permalink
Add tests for userdata
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Ellis (OpenFaaS Ltd) <[email protected]>
  • Loading branch information
alexellis committed Feb 5, 2020
1 parent f7366ed commit cbf3731
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 53 deletions.
38 changes: 38 additions & 0 deletions cmd/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright (c) Inlets Author(s) 2019. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

package cmd

import (
"fmt"
"io/ioutil"
"strings"

"github.com/pkg/errors"
"github.com/spf13/pflag"
)

func getFileOrString(flags *pflag.FlagSet, file, value string, required bool) (string, error) {
var val string
fileVal, _ := flags.GetString(file)
if len(fileVal) > 0 {
res, err := ioutil.ReadFile(fileVal)
if err != nil {
return "", err
}
val = strings.TrimSpace(string(res))
} else {

flagVal, err := flags.GetString(value)
if err != nil {
return "", errors.Wrap(err, "failed to get '"+value+"' value.")
}
val = flagVal
}

if required && len(val) == 0 {
return "", fmt.Errorf("give a value for --%s or --%s", file, value)
}

return val, nil
}
80 changes: 27 additions & 53 deletions cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package cmd
import (
"encoding/base64"
"fmt"
"io/ioutil"
"strconv"
"strings"
"time"
Expand All @@ -17,11 +16,15 @@ import (
"github.com/pkg/errors"
password "github.com/sethvargo/go-password/password"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)

const inletsControlPort = 8080
const inletsProControlPort = 8123

func init() {

inletsCmd.AddCommand(createCmd)

createCmd.Flags().StringP("provider", "p", "digitalocean", "The cloud provider - digitalocean, gce, ec2, packet, scaleway, or civo")
createCmd.Flags().StringP("region", "r", "lon1", "The region for your cloud provider")
createCmd.Flags().StringP("zone", "z", "us-central1-a", "The zone for the exit node (Google Compute Engine)")
Expand Down Expand Up @@ -144,11 +147,8 @@ func runCreate(cmd *cobra.Command, _ []string) error {
pro = true
}


name := strings.Replace(names.GetRandomName(10), "_", "-", -1)

inletsControlPort := 8080

userData := makeUserdata(inletsToken, inletsControlPort, remoteTCP)

projectID, _ := cmd.Flags().GetString("project-id")
Expand Down Expand Up @@ -202,7 +202,6 @@ To Delete:
return nil
}

proPort := 8123
fmt.Printf(`inlets-pro exit-node summary:
IP: %s
Auth-token: %s
Expand All @@ -218,7 +217,7 @@ Command:
To Delete:
inletsctl delete --provider %s --id "%s"
`,
hostStatus.IP, inletsToken, hostStatus.IP, proPort, inletsToken, provider, hostStatus.ID)
hostStatus.IP, inletsToken, hostStatus.IP, inletsProControlPort, inletsToken, provider, hostStatus.ID)

return nil
}
Expand Down Expand Up @@ -313,7 +312,7 @@ func createHost(provider, name, region, zone, projectID, userData, inletsPort st
UserData: base64.StdEncoding.EncodeToString([]byte(userData)),
Additional: map[string]string{
"inlets-port": inletsPort,
"pro": fmt.Sprint(pro),
"pro": fmt.Sprint(pro),
},
}, nil
}
Expand All @@ -332,52 +331,27 @@ export CONTROLPORT="` + controlPort + `"
curl -sLS https://get.inlets.dev | sh
curl -sLO https://raw.githubusercontent.com/inlets/inlets/master/hack/inlets-operator.service && \
mv inlets-operator.service /etc/systemd/system/inlets.service && \
echo "AUTHTOKEN=$AUTHTOKEN" > /etc/default/inlets && \
echo "CONTROLPORT=$CONTROLPORT" >> /etc/default/inlets && \
systemctl start inlets && \
systemctl enable inlets`
mv inlets-operator.service /etc/systemd/system/inlets.service && \
echo "AUTHTOKEN=$AUTHTOKEN" > /etc/default/inlets && \
echo "CONTROLPORT=$CONTROLPORT" >> /etc/default/inlets && \
systemctl start inlets && \
systemctl enable inlets`
}

return `#!/bin/bash
export AUTHTOKEN="` + authToken + `"
export REMOTETCP="` + remoteTCP + `"
export IP=$(curl -sfSL https://ifconfig.co)
curl -SLsf https://github.com/inlets/inlets-pro/releases/download/0.4.3/inlets-pro > /tmp/inlets-pro && \
chmod +x /tmp/inlets-pro && \
mv /tmp/inlets-pro /usr/local/bin/inlets-pro
curl -sLO https://raw.githubusercontent.com/inlets/inlets/master/hack/inlets-pro.service && \
mv inlets-pro.service /etc/systemd/system/inlets-pro.service && \
echo "AUTHTOKEN=$AUTHTOKEN" >> /etc/default/inlets-pro && \
echo "REMOTETCP=$REMOTETCP" >> /etc/default/inlets-pro && \
echo "IP=$IP" >> /etc/default/inlets-pro && \
systemctl start inlets-pro && \
systemctl enable inlets-pro`
}

func getFileOrString(flags *pflag.FlagSet, file, value string, required bool) (string, error) {
var val string
fileVal, _ := flags.GetString(file)
if len(fileVal) > 0 {
res, err := ioutil.ReadFile(fileVal)
if err != nil {
return "", err
}
val = strings.TrimSpace(string(res))
} else {

flagVal, err := flags.GetString(value)
if err != nil {
return "", errors.Wrap(err, "failed to get '"+value+"' value.")
}
val = flagVal
}

if required && len(val) == 0 {
return "", fmt.Errorf("give a value for --%s or --%s", file, value)
}

return val, nil
export AUTHTOKEN="` + authToken + `"
export REMOTETCP="` + remoteTCP + `"
export IP=$(curl -sfSL https://ifconfig.co)
curl -SLsf https://github.com/inlets/inlets-pro/releases/download/0.4.3/inlets-pro > /tmp/inlets-pro && \
chmod +x /tmp/inlets-pro && \
mv /tmp/inlets-pro /usr/local/bin/inlets-pro
curl -sLO https://raw.githubusercontent.com/inlets/inlets/master/hack/inlets-pro.service && \
mv inlets-pro.service /etc/systemd/system/inlets-pro.service && \
echo "AUTHTOKEN=$AUTHTOKEN" >> /etc/default/inlets-pro && \
echo "REMOTETCP=$REMOTETCP" >> /etc/default/inlets-pro && \
echo "IP=$IP" >> /etc/default/inlets-pro && \
systemctl start inlets-pro && \
systemctl enable inlets-pro`
}
53 changes: 53 additions & 0 deletions cmd/create_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright (c) Inlets Author(s) 2019. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

package cmd

import (
"testing"
)

func Test_makeUserdata_InletsOSS(t *testing.T) {
userData := makeUserdata("auth", inletsControlPort, "")

wantUserdata := `#!/bin/bash
export AUTHTOKEN="auth"
export CONTROLPORT="8080"
curl -sLS https://get.inlets.dev | sh
curl -sLO https://raw.githubusercontent.com/inlets/inlets/master/hack/inlets-operator.service && \
mv inlets-operator.service /etc/systemd/system/inlets.service && \
echo "AUTHTOKEN=$AUTHTOKEN" > /etc/default/inlets && \
echo "CONTROLPORT=$CONTROLPORT" >> /etc/default/inlets && \
systemctl start inlets && \
systemctl enable inlets`

if userData != wantUserdata {
t.Errorf("want:\n%s\nbut got:\n%s", wantUserdata, userData)
}
}

func Test_makeUserdata_InletsPro(t *testing.T) {
userData := makeUserdata("auth", inletsProControlPort, "localhost")

wantUserdata := `#!/bin/bash
export AUTHTOKEN="auth"
export REMOTETCP="localhost"
export IP=$(curl -sfSL https://ifconfig.co)
curl -SLsf https://github.com/inlets/inlets-pro/releases/download/0.4.3/inlets-pro > /tmp/inlets-pro && \
chmod +x /tmp/inlets-pro && \
mv /tmp/inlets-pro /usr/local/bin/inlets-pro
curl -sLO https://raw.githubusercontent.com/inlets/inlets/master/hack/inlets-pro.service && \
mv inlets-pro.service /etc/systemd/system/inlets-pro.service && \
echo "AUTHTOKEN=$AUTHTOKEN" >> /etc/default/inlets-pro && \
echo "REMOTETCP=$REMOTETCP" >> /etc/default/inlets-pro && \
echo "IP=$IP" >> /etc/default/inlets-pro && \
systemctl start inlets-pro && \
systemctl enable inlets-pro`

if userData != wantUserdata {
t.Errorf("want: %s, but got: %s", wantUserdata, userData)
}
}

0 comments on commit cbf3731

Please sign in to comment.