Refactor random generation into a definition to make it reusable

Signed-off-by: Tom Chauveau <tom.chauveau@epitech.eu>
This commit is contained in:
Tom Chauveau 2021-06-07 23:23:16 +02:00
parent e3ee7f2568
commit fdb91a7332
20 changed files with 288 additions and 216 deletions

View File

@ -11,8 +11,10 @@ TestConfig: awsConfig: aws.#Config & {
}
TestECR: {
random: #Random & {}
repository: "125635003186.dkr.ecr.\(TestConfig.awsConfig.region).amazonaws.com/dagger-ci"
tag: "test-ecr-\(random)"
tag: "test-ecr-\(random.out)"
creds: ecr.#Credentials & {
config: TestConfig.awsConfig
@ -25,7 +27,7 @@ TestECR: {
op.#DockerBuild & {
dockerfile: """
FROM alpine
RUN echo \(random) > /test
RUN echo \(random.out) > /test
"""
},
@ -61,7 +63,7 @@ TestECR: {
op.#Exec & {
always: true
args: [
"sh", "-c", "test $(cat test) = \(random)",
"sh", "-c", "test $(cat test) = \(random.out)",
]
},
]
@ -76,7 +78,7 @@ TestECR: {
op.#DockerBuild & {
dockerfile: #"""
FROM \#(push.ref)
RUN test $(cat test) = \#(random)
RUN test $(cat test) = \#(random.out)
"""#
},
]

View File

@ -1,21 +1,33 @@
package ecr
import (
"strconv"
"dagger.io/alpine"
"dagger.io/dagger/op"
)
// Generate a random number
random: {
#Random: {
size: *12 | number
out: {
string
#up: [
op.#Load & {from: alpine.#Image},
op.#Exec & {
always: true
args: ["sh", "-c", "cat /dev/urandom | tr -dc 'a-z' | fold -w 10 | head -n 1 | tr -d '\n' > /rand"]
args: ["sh", "-c", #"""
tr -cd '[:alpha:]' < /dev/urandom | fold -w "$SIZE" | head -n 1 | tr '[A-Z]' '[a-z]' | tr -d '\n' > /rand
"""#,
]
env: SIZE: strconv.FormatInt(size, 10)
},
op.#Export & {
source: "/rand"
},
]
}
}

View File

@ -15,7 +15,9 @@ registry: {
}
TestPushAndPull: {
ref: "daggerio/ci-test:\(random)"
random: #Random & {}
ref: "daggerio/ci-test:\(random.out)"
// Create image
image: docker.#ImageFromDockerfile & {

View File

@ -1,21 +1,33 @@
package docker
import (
"strconv"
"dagger.io/alpine"
"dagger.io/dagger/op"
)
// Generate a random number
random: {
#Random: {
size: *12 | number
out: {
string
#up: [
op.#Load & {from: alpine.#Image},
op.#Exec & {
always: true
args: ["sh", "-c", "cat /dev/urandom | tr -dc 'a-z' | fold -w 10 | head -n 1 | tr -d '\n' > /rand"]
args: ["sh", "-c", #"""
tr -cd '[:alpha:]' < /dev/urandom | fold -w "$SIZE" | head -n 1 | tr '[A-Z]' '[a-z]' | tr -d '\n' > /rand
"""#,
]
env: SIZE: strconv.FormatInt(size, 10)
},
op.#Export & {
source: "/rand"
},
]
}
}

View File

@ -9,11 +9,15 @@ key: dagger.#Secret @dagger(input)
passphrase: dagger.#Secret @dagger(input)
user: string @dagger(input)
TestRun: run: docker.#Run & {
TestRun: {
random: #Random & {}
run: docker.#Run & {
host: "143.198.64.230"
ref: "nginx:alpine"
"user": user
"passphrase": passphrase
name: "daggerci-test-simple-\(random)"
name: "daggerci-test-simple-\(random.out)"
"key": key
}
}

View File

@ -1,20 +1,33 @@
package docker
import (
"dagger.io/dagger/op"
"strconv"
"dagger.io/alpine"
"dagger.io/dagger/op"
)
random: {
#Random: {
size: *12 | number
out: {
string
#up: [
op.#Load & {from: alpine.#Image},
op.#Exec & {
always: true
args: ["sh", "-c", "cat /dev/urandom | tr -dc 'a-z' | fold -w 10 | head -n 1 | tr -d '\n' > /rand"]
args: ["sh", "-c", #"""
tr -cd '[:alpha:]' < /dev/urandom | fold -w "$SIZE" | head -n 1 | tr '[A-Z]' '[a-z]' | tr -d '\n' > /rand
"""#,
]
env: SIZE: strconv.FormatInt(size, 10)
},
op.#Export & {
source: "/rand"
},
]
}
}

View File

@ -1,20 +1,33 @@
package docker
import (
"dagger.io/dagger/op"
"strconv"
"dagger.io/alpine"
"dagger.io/dagger/op"
)
random: {
#Random: {
size: *12 | number
out: {
string
#up: [
op.#Load & {from: alpine.#Image},
op.#Exec & {
always: true
args: ["sh", "-c", "cat /dev/urandom | tr -dc 'a-z' | fold -w 10 | head -n 1 | tr -d '\n' > /rand"]
args: ["sh", "-c", #"""
tr -cd '[:alpha:]' < /dev/urandom | fold -w "$SIZE" | head -n 1 | tr '[A-Z]' '[a-z]' | tr -d '\n' > /rand
"""#,
]
env: SIZE: strconv.FormatInt(size, 10)
},
op.#Export & {
source: "/rand"
},
]
}
}

View File

@ -7,10 +7,14 @@ import (
key: dagger.#Secret @dagger(input)
TestRun: run: docker.#Run & {
TestRun: {
random: #Random & {}
run: docker.#Run & {
host: "143.198.64.230"
ref: "nginx:alpine"
user: "root"
name: "daggerci-test-simple-\(random)"
name: "daggerci-test-simple-\(random.out)"
"key": key
}
}

View File

@ -1,20 +1,33 @@
package docker
import (
"dagger.io/dagger/op"
"strconv"
"dagger.io/alpine"
"dagger.io/dagger/op"
)
random: {
#Random: {
size: *12 | number
out: {
string
#up: [
op.#Load & {from: alpine.#Image},
op.#Exec & {
always: true
args: ["sh", "-c", "cat /dev/urandom | tr -dc 'a-z' | fold -w 10 | head -n 1 | tr -d '\n' > /rand"]
args: ["sh", "-c", #"""
tr -cd '[:alpha:]' < /dev/urandom | fold -w "$SIZE" | head -n 1 | tr '[A-Z]' '[a-z]' | tr -d '\n' > /rand
"""#,
]
env: SIZE: strconv.FormatInt(size, 10)
},
op.#Export & {
source: "/rand"
},
]
}
}

View File

@ -10,11 +10,15 @@ key: dagger.#Secret @dagger(input)
passphrase: dagger.#Secret @dagger(input)
user: string @dagger(input)
TestRun: run: docker.#Run & {
TestRun: {
random: #Random & {}
run: docker.#Run & {
host: "143.198.64.230"
ref: "nginx:alpine"
"user": user
"passphrase": passphrase
name: "daggerci-test-simple-\(random)"
name: "daggerci-test-simple-\(random.out)"
"key": key
}
}

View File

@ -9,8 +9,10 @@ import (
TestConfig: gcpConfig: gcp.#Config
TestGCR: {
random: #Random & {}
repository: "gcr.io/dagger-ci/test"
tag: "test-gcr-\(random)"
tag: "test-gcr-\(random.out)"
creds: gcr.#Credentials & {
config: TestConfig.gcpConfig
@ -23,7 +25,7 @@ TestGCR: {
op.#DockerBuild & {
dockerfile: """
FROM alpine
RUN echo \(random) > /test
RUN echo \(random.out) > /test
"""
},
@ -59,7 +61,7 @@ TestGCR: {
op.#Exec & {
always: true
args: [
"sh", "-c", "test $(cat test) = \(random)",
"sh", "-c", "test $(cat test) = \(random.out)",
]
},
]
@ -74,7 +76,7 @@ TestGCR: {
op.#DockerBuild & {
dockerfile: #"""
FROM \#(push.ref)
RUN test $(cat test) = \#(random)
RUN test $(cat test) = \#(random.out)
"""#
},
]

View File

@ -1,20 +1,33 @@
package gcr
import (
"strconv"
"dagger.io/alpine"
"dagger.io/dagger/op"
)
// Generate a random number
random: {
#Random: {
size: *12 | number
out: {
string
#up: [
op.#Load & {from: alpine.#Image},
op.#Exec & {
args: ["sh", "-c", "cat /dev/urandom | tr -dc 'a-z' | fold -w 10 | head -n 1 | tr -d '\n' > /rand"]
always: true
args: ["sh", "-c", #"""
tr -cd '[:alpha:]' < /dev/urandom | fold -w "$SIZE" | head -n 1 | tr '[A-Z]' '[a-z]' | tr -d '\n' > /rand
"""#,
]
env: SIZE: strconv.FormatInt(size, 10)
},
op.#Export & {
source: "/rand"
},
]
}
}

View File

@ -11,9 +11,11 @@ kubeconfig: dagger.#Secret @dagger(input)
// Deploy user local chart
TestHelmSimpleChart: {
random: #Random & {}
// Deploy chart
deploy: helm.#Chart & {
name: "dagger-test-helm-simple-chart-\(random)"
name: "dagger-test-helm-simple-chart-\(random.out)"
namespace: "dagger-test"
"kubeconfig": kubeconfig
chartSource: dagger.#Artifact
@ -28,9 +30,11 @@ TestHelmSimpleChart: {
// Deploy remote chart
TestHelmRepoChart: {
random: #Random & {}
// Deploy chart
deploy: helm.#Chart & {
name: "dagger-test-helm-repository-\(random)"
name: "dagger-test-helm-repository-\(random.out)"
namespace: "dagger-test"
"kubeconfig": kubeconfig
chart: "redis"

View File

@ -1,21 +1,33 @@
package helm
import (
"strconv"
"dagger.io/alpine"
"dagger.io/dagger/op"
)
// Generate a random number
random: {
#Random: {
size: *12 | number
out: {
string
#up: [
op.#Load & {from: alpine.#Image},
op.#Exec & {
always: true
args: ["sh", "-c", "cat /dev/urandom | tr -dc 'a-z' | fold -w 10 | head -n 1 | tr -d '\n' > /rand"]
args: ["sh", "-c", #"""
tr -cd '[:alpha:]' < /dev/urandom | fold -w "$SIZE" | head -n 1 | tr '[A-Z]' '[a-z]' | tr -d '\n' > /rand
"""#,
]
env: SIZE: strconv.FormatInt(size, 10)
},
op.#Export & {
source: "/rand"
},
]
}
}

View File

@ -11,11 +11,13 @@ import (
kubeconfig: dagger.#Secret @dagger(input)
TestKubeApply: {
random: #Random & {}
// Pod spec
kubeSrc: {
apiVersion: "v1"
kind: "Pod"
metadata: name: "kube-test-\(random)"
metadata: name: "kube-test-\(random.out)"
spec: {
restartPolicy: "Never"
containers: [{

View File

@ -1,21 +1,33 @@
package kubernetes
import (
"strconv"
"dagger.io/alpine"
"dagger.io/dagger/op"
)
// Generate a random number
random: {
#Random: {
size: *12 | number
out: {
string
#up: [
op.#Load & {from: alpine.#Image},
op.#Exec & {
always: true
args: ["sh", "-c", "cat /dev/urandom | tr -dc 'a-z' | fold -w 10 | head -n 1 | tr -d '\n' > /rand"]
args: ["sh", "-c", #"""
tr -cd '[:alpha:]' < /dev/urandom | fold -w "$SIZE" | head -n 1 | tr '[A-Z]' '[a-z]' | tr -d '\n' > /rand
"""#,
]
env: SIZE: strconv.FormatInt(size, 10)
},
op.#Export & {
source: "/rand"
},
]
}
}

View File

@ -7,10 +7,12 @@ import (
)
TestNetlify: {
random: #Random & {}
// Generate a website containing the random number
html: #up: [
op.#WriteFile & {
content: random
content: random.out
dest: "index.html"
},
]
@ -38,7 +40,7 @@ TestNetlify: {
"pipefail",
"-c",
#"""
test "$(curl \#(deploy.deployUrl))" = "\#(random)"
test "$(curl \#(deploy.deployUrl))" = "\#(random.out)"
"""#,
]
},

View File

@ -1,20 +1,33 @@
package netlify
import (
"strconv"
"dagger.io/alpine"
"dagger.io/dagger/op"
)
// Generate a random number
random: {
#Random: {
size: *12 | number
out: {
string
#up: [
op.#Load & {from: alpine.#Image},
op.#Exec & {
args: ["sh", "-c", "cat /dev/urandom | tr -dc 'a-z' | fold -w 10 | head -n 1 | tr -d '\n' > /rand"]
always: true
args: ["sh", "-c", #"""
tr -cd '[:alpha:]' < /dev/urandom | fold -w "$SIZE" | head -n 1 | tr '[A-Z]' '[a-z]' | tr -d '\n' > /rand
"""#,
]
env: SIZE: strconv.FormatInt(size, 10)
},
op.#Export & {
source: "/rand"
},
]
}
}

View File

@ -1,46 +0,0 @@
package netlify
import (
"dagger.io/dagger/op"
"dagger.io/alpine"
"dagger.io/netlify"
)
TestNetlify: {
// Generate a website containing the random number
html: #up: [
op.#WriteFile & {
content: random
dest: "index.html"
},
]
// Deploy to netlify
deploy: netlify.#Site & {
contents: html
name: "dagger-test"
}
// Check if the deployed site has the random marker
check: #up: [
op.#Load & {
from: alpine.#Image & {
package: bash: "=~5.1"
package: curl: true
}
},
op.#Exec & {
args: [
"/bin/bash",
"--noprofile",
"--norc",
"-eo",
"pipefail",
"-c",
#"""
test "$(curl \#(deploy.deployUrl))" = "\#(random)"
"""#,
]
},
]
}

View File

@ -1,21 +0,0 @@
package netlify
import (
"dagger.io/alpine"
"dagger.io/dagger/op"
)
// Generate a random number
random: {
string
#up: [
op.#Load & {from: alpine.#Image},
op.#Exec & {
always: true
args: ["sh", "-c", "cat /dev/urandom | tr -dc 'a-z' | fold -w 10 | head -n 1 | tr -d '\n' > /rand"]
},
op.#Export & {
source: "/rand"
},
]
}