Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Testing Wercker plugin with bats

TAKAHASHI Kazunari
November 29, 2014
460

Testing Wercker plugin with bats

TAKAHASHI Kazunari

November 29, 2014
Tweet

Transcript

  1. !TZP
    5PLZV3VCZ,BJHJ
    5FTUJOH
    8FSDLFSQMVHJO
    XJUICBUT

    View full-size slide

  2. ͓஌Βͤ
    ౦ژ3VCZ๨೥ձ
    4BU
    ๺ւಓ্໺޿খ࿏ళ
    ߴڮձ௕ͱ೥Λ࿑͓͏

    View full-size slide

  3. 8FSDLFSQMVHJOʹ
    CBUTͰ
    ςετΛॻ͘

    View full-size slide

  4. 8FSLFSͱ͸
    5IF0QFOEFMJWFSZQMBUGPSN
    ϓϥΠϕʔτϦϙδτϦͷ$*͕ແྉ
    QMVHJOΛ3VCZ΍#BTIͰ࡞੒
    νϟοτʹ$*ͷ݁ՌΛ௨஌

    View full-size slide

  5. CBUTͱ͸ʁ
    #BTI"VUPNBUFE5FTUJOH4ZTUFN
    SCFOW SVCZCVJMEͰར༻

    View full-size slide

  6. γΣϧεΫϦϓτͷ
    ςετͷ࿩Ͱ͢

    View full-size slide

  7. JEPCBUBOPUJGZ

    View full-size slide

  8. JEPCBUBOPUJGZ
    ೥݄WϦϦʔε
    JEPCBUBʹ$*ͷ݁ՌΛ௨஌
    CVJMEEFQMPZʹରԠ
    ͍͢͝؆୯ͳ4IFMM4DSJQU

    View full-size slide

  9. 1MVHJOͷલఏ৚݅
    XFSDLFSͷ؀ڥม਺ ݸ͘Β͍

    XFSDLFSͷGVODUJPO
    QMVHJOͷ؀ڥม਺

    View full-size slide

  10. 1MVHJOͷ΍ͬͯΔ͜ͱ
    ඞਢͷ؀ڥม਺ΛνΣοΫ
    50,&/
    ؀ڥม਺͔Β৘ใऩूͯ͠จݴ࡞੒
    CVJMEͷ੒൱
    DVSMͰ1045

    View full-size slide

  11. 1MVHJOΛςετ͢Δૂ͍
    લఏ৚݅Λίʔυʹ͍ͨ͠
    લఏ৚݅Λίʔυʹ͍ͨ͠
    લఏ৚݅Λίʔυʹ͍ͨ͠
    લఏ৚݅Λίʔυʹ͍ͨ͠
    ύϥϝʔλΛม͑ͯݕূ͍ͨ͠

    View full-size slide

  12. ςετ͕ඞཁͳΒ3VCZ࢖͑͹͍
    ͍͡ΌΜͱࢥ͏͚ΕͲ΋ɺϓϩμ
    Ϋγϣϯίʔυ͸௒γϯϓϧͳ༁
    ͩ͠ɺલఏ৚݅ΛίʔυʹͰ͖Ε
    ͹͍͍ͷ͔ͩΒɺ4IFMM4DSJQU
    Ͱ΋ߦ͚ΔΜ͡Όͳ͍ߟ͑ͨ

    View full-size slide

  13. ײ૝
    લఏ৚݅ΛίʔυʹͰ͖ͨ
    ׳ΕΔ·Ͱ͕͔͔࣌ؒͬͨ
    TUVC໰୊͕ະղܾ
    4IFMM4DSJQUྗऑ͍
    ݱঢ়ͩͱ3VCZͰ΍ͬͨ΄͏ָ͕

    View full-size slide

  14. SVOTI
    source "$WERCKER_STEP_ROOT/functions.sh"!
    !
    error_message=$(valid)!
    if [ $? = 1 ]; then!
    warn "$error_message"!
    exit 0!
    fi!
    !
    http_code=`curl \!
    --silent \!
    --data "format=html" \!
    --data-urlencode "source=$(message)" \!
    --output "$WERCKER_STEP_TEMP/result.txt" \!
    --write-out "%{http_code}" \!
    "https://idobata.io/hook/$WERCKER_IDOBATA_NOTIFY_TOKEN"`!
    !
    if [ "$http_code" = "200" ]; then!
    success "Finished successfully!"!
    else!
    warn `cat $WERCKER_STEP_TEMP/result.txt`!
    warn "Finished unsuccessfully."!
    fi

    View full-size slide

  15. GVODUJPOTTI ൈਮ

    valid() {!
    if [ ! -n "$WERCKER_IDOBATA_NOTIFY_TOKEN" ]; then!
    echo 'Please specify token property.'!
    exit 1!
    fi!
    }!
    !
    build_result() {!
    result=$(tr '[a-z]' '[A-Z]' <<<"$WERCKER_RESULT")!
    if [ "$WERCKER_RESULT" = "passed" ]; then!
    echo "$result"!
    else!
    echo "$result"!
    fi!
    }

    View full-size slide

  16. UFTUUFTU@IFMQFSCBTI

    View full-size slide

  17. UFTUUFTU@IFMQFSCBTI
    export PATH="$BATS_TEST_DIRNAME/..:$PATH"!
    !
    if [ ! -n "$WERCKER" ]; then!
    export WERCKER_GIT_DOMAIN="github.com"!
    export WERCKER_GIT_OWNER="1syo"!
    export WERCKER_GIT_REPOSITORY="wercker-step-idobata-notify"!
    export WERCKER_GIT_BRANCH="master"!
    export WERCKER_GIT_COMMIT="aff2f780d59346c0386d59db6aac62e02be2005a"!
    export WERCKER_STARTED_BY="TAKAHASHI Kazunari"!
    export WERCKER_APPLICATION_ID="5253e1053673929130009361"!
    export WERCKER_APPLICATION_NAME="wercker-step-idobata-notify"!
    export WERCKER_APPLICATION_OWNER_NAME="1syo"!
    export WERCKER_APPLICATION_URL=\!
    ! ! ! ”https://app.wercker.com/#applications/5253e1053673929130009361"!
    export WERCKER_STEP_ROOT="."!
    export WERCKER_STEP_TEMP=$BATS_TMPDIR!
    fi

    View full-size slide

  18. UFTUUFTU@IFMQFSCBTI
    success() {!
    echo $1!
    }!
    !
    fail() {!
    echo $1!
    exit 1!
    }!
    !
    warn() {!
    echo $1!
    }!
    !
    export -f success!
    export -f fail!
    export -f warn

    View full-size slide

  19. %FWDFOUFS
    &OWJSPONFOU7BSJBCMFT
    8SJUJOHPVUQVU
    1SFEFpOE7BSJBCMFT

    View full-size slide

  20. UFTUUFTU@IFMQFSCBTI
    stub() {!
    local cmd=$1!
    local ret=$2!
    eval "$(echo -e "${cmd}() {\n echo $ret\n}")"!
    export -f ${cmd}!
    }!
    !
    unstub() {!
    local cmd=$1!
    unset -f "$cmd"!
    }

    View full-size slide

  21. TUVC

    ୈҰҾ਺ʹTUVC͍ͨ͠ίϚϯυ
    ୈೋҾ਺ʹίϚϯυͷ࣮ߦ݁Ռ
    TUVCDVSM

    View full-size slide

  22. UFTUSVOCBUT
    #!/usr/bin/env bats!
    load test_helper!
    !
    @test "Token undefined" {!
    WERCKER_IDOBATA_NOTIFY_TOKEN="" \!
    WERCKER_STEP_ROOT="." \!
    run run.sh!
    !
    [ "$status" = "0" ]!
    [ "$output" = "Please specify token property." ]!
    }

    View full-size slide

  23. ςετͷߏ੒
    ධՁ͍ͨ͠؀ڥม਺͸FYQPSU͠ͳ͍
    TUBUVTʹऴྃίʔυ
    PVUQVUʹඪ४ग़ྗ͔ඪ४Τϥʔ

    View full-size slide

  24. UFTUSVOCBUT
    @test "Send a message" {!
    stub curl 200!
    !
    WERCKER_IDOBATA_NOTIFY_TOKEN=token \!
    WERCKER_BUILD_URL="http://example.com/build/1" \!
    WERCKER_BUILD_ID=1 \!
    WERCKER_RESULT="passed" \!
    WERCKER_STEP_ROOT="." \!
    CI=true \!
    run run.sh!
    !
    [ "$status" = "0" ]!
    [ "$output" = "Finished successfully!" ]!
    unstub curl!
    }

    View full-size slide

  25. UFTUSVOCBUT
    @test "Can not send a message" {!
    echo "Error" > "$WERCKER_STEP_TEMP/result.txt"!
    stub curl 500!
    !
    WERCKER_IDOBATA_NOTIFY_TOKEN=token \!
    WERCKER_RESULT="passed" \!
    WERCKER_STEP_ROOT="." \!
    CI=true \!
    run run.sh!
    !
    [ "$status" = "0" ]!
    [ "${lines[0]}" = "Error" ]!
    [ "${lines[1]}" = "Finished unsuccessfully." ]!
    unstub curl!
    !
    rm "$WERCKER_STEP_TEMP/result.txt"!
    }

    View full-size slide

  26. CBUTUFTU
    㾎5PLFOVOEFpOFE
    㾎/PUJGZTLJQ
    㾎#VJME1BTTFE
    㾎%FQMPZQBTTFE
    㾎#VJMEGBJMVSF
    㾎%FQMPZGBJMVSF
    㾎5PLFOVOEFpOFE
    㾎4FOEBNFTTBHF
    㾎$BOOPUTFOEB
    NFTTBHF
    !
    UFTUT GBJMVSFT

    View full-size slide

  27. ײ૝
    લఏ৚݅ΛίʔυʹͰ͖ͨ
    ׳ΕΔ·Ͱ͕͔͔࣌ؒͬͨ
    4IFMM4DSJQUྗऑ͍
    TUVC໰୊͕ະղܾ
    ݱঢ়ͩͱ3VCZͰ΍ͬͨ΄͏ָ͕

    View full-size slide

  28. -FU`TXSJUJOH
    XSFDLFSQMVHJO
    XJUICBUT

    View full-size slide