|
| 1 | +/* |
| 2 | + * Copyright (c) 2021 The StreamX Project |
| 3 | + * <p> |
| 4 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 5 | + * or more contributor license agreements. See the NOTICE file |
| 6 | + * distributed with this work for additional information |
| 7 | + * regarding copyright ownership. The ASF licenses this file |
| 8 | + * to you under the Apache License, Version 2.0 (the |
| 9 | + * "License"); you may not use this file except in compliance |
| 10 | + * with the License. You may obtain a copy of the License at |
| 11 | + * <p> |
| 12 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | + * <p> |
| 14 | + * Unless required by applicable law or agreed to in writing, |
| 15 | + * software distributed under the License is distributed on an |
| 16 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 17 | + * KIND, either express or implied. See the License for the |
| 18 | + * specific language governing permissions and limitations |
| 19 | + * under the License. |
| 20 | + */ |
| 21 | +package com.streamxhub.streamx.codebuild |
| 22 | + |
| 23 | +import com.streamxhub.streamx.codebuild.FlinkDockerfileTemplate.DEFAULT_DOCKER_FILE_NAME |
| 24 | +import org.apache.commons.io.FileUtils |
| 25 | +import org.scalatest.BeforeAndAfter |
| 26 | +import org.scalatest.matchers.must.Matchers |
| 27 | +import org.scalatest.wordspec.AnyWordSpec |
| 28 | + |
| 29 | +import java.io.File |
| 30 | + |
| 31 | +class FlinkDockerfileTemplateSpec extends AnyWordSpec with BeforeAndAfter with Matchers { |
| 32 | + |
| 33 | + val outputDir = new File("FlinkDockerfileTemplateSpec-output/") |
| 34 | + |
| 35 | + val assertDockerFileContent = |
| 36 | + """FROM 1.13-scala_2.11 |
| 37 | + |RUN mkdir -p $FLINK_HOME/usrlib |
| 38 | + |COPY /WordCountSQL.jar $FLINK_HOME/usrlib/WordCountSQL.jar |
| 39 | + |""".stripMargin |
| 40 | + |
| 41 | + before { |
| 42 | + outputDir.mkdir() |
| 43 | + } |
| 44 | + after { |
| 45 | + FileUtils.forceDelete(outputDir) |
| 46 | + } |
| 47 | + |
| 48 | + "FlinkDockerfileTemplate" when { |
| 49 | + |
| 50 | + "create dockerfile" should { |
| 51 | + val template = new FlinkDockerfileTemplate("1.13-scala_2.11", path("flink/WordCountSQL.jar")) |
| 52 | + val assertDockerFileContent = |
| 53 | + """FROM 1.13-scala_2.11 |
| 54 | + |RUN mkdir -p $FLINK_HOME/usrlib |
| 55 | + |COPY /WordCountSQL.jar $FLINK_HOME/usrlib/WordCountSQL.jar |
| 56 | + |""".stripMargin |
| 57 | + |
| 58 | + "build Dockerfile content" in { |
| 59 | + template.dockerfileContent mustBe assertDockerFileContent |
| 60 | + } |
| 61 | + "write Dockerfile to file" in { |
| 62 | + val output = outputDir.getAbsolutePath + "/my-dockerfile" |
| 63 | + val outFile = template.writeDockerfile(output) |
| 64 | + FileUtils.readFileToString(outFile, "UTF-8") mustBe assertDockerFileContent |
| 65 | + } |
| 66 | + "write Dockerfile to directory" in { |
| 67 | + val outFile = template.writeDockerfile(outputDir.getAbsolutePath) |
| 68 | + outFile.getName mustBe DEFAULT_DOCKER_FILE_NAME |
| 69 | + FileUtils.readFileToString(outFile, "UTF-8") mustBe assertDockerFileContent |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + } |
| 74 | + |
| 75 | +} |
0 commit comments